跨域 AJAX 请求在 Android 手机的本机浏览器上表现异常
我的设置是这样的:
- SQLite 数据库位于我的 Apache 文件夹之一
- Perl cgi 脚本位于同一文件夹中,具有公共权限,以便它可以处理数据库交互
- 运行在同一台计算机上的 C 程序侦听特定的用于特定 JSON 请求的端口,并以特定格式的 JSON 数据进行响应
主要用 jQuery 和 HTML 编写的网页必须不断地访问 SQLite 数据库和 C 程序。它使用 AJAX 请求来实现这一点——这是代码中的一个示例:
function sendCommand(cmd, callback) {
$.ajax({url: "http://192.168.42.90:6112/?cmd=" + cmd,
dataType: "jsonp",
success: function(d,s,x) {
callback(d);
},
jsonpCallback: "json",
error: function(xhr) {
alert('Error: ' + xhr.status);
}
});
}
所以我可以使用类似 sendCommand("get_x", updatePosition) 的方法来调用它——以便它将 get_x 命令发送到 C 程序,并且,如果得到良好的反馈,则通过 updatePosition 函数运行所述反馈。
因此,如果我在同一台机器上的网页上运行,所有这些都会非常有效。如果我转移到另一台机器——在本例中,具体来说是一部 Android 手机,通过 USB 进行绑定 IP,它……有点管用。这就是令我困惑的地方——它不会像我习惯的 JavaScript 那样默默地失败。相反,据我所知,它发送了请求,声称已返回数据,并运行回调函数——只是,根据 C 程序,什么也没有发生;根据 Wireshark 的说法,没有发送任何数据包;它返回的“数据”是一个空的、特殊格式的字符串。如果我期望 C 的输出为“(5,5)”,那么它会得到“(0,0)”——为什么它会得到任何东西呢?更不用说用 0 填充“(,)”了?
很奇怪。我尝试了各种小事情,例如将数据类型更改为“json”,确保我的所有权限设置正确,等等。我相当确定 Apache 不会干扰,因为日志中没有任何内容 - 什么让我困惑的是我在 Wireshark 上什么也没看到。就好像电话根本就没有尝试提出请求一样。
My setup is like so:
- An SQLite database sits in one of my Apache folders
- A Perl cgi script sits in the same folder, with public permissions, so that it can handle database interactions
- A C program, running on the same machine, listens on a specific port for specific JSON requests and responds with specifically-formatted JSON data
A webpage, written primarily in jQuery and HTML, must access both the SQLite database and the C program somewhat constantly. It does so using AJAX requests -- here's an example from the code:
function sendCommand(cmd, callback) {
$.ajax({url: "http://192.168.42.90:6112/?cmd=" + cmd,
dataType: "jsonp",
success: function(d,s,x) {
callback(d);
},
jsonpCallback: "json",
error: function(xhr) {
alert('Error: ' + xhr.status);
}
});
}
So I might invoke it with something like sendCommand("get_x", updatePosition) -- so that it'd send the get_x command along to the C program, and, if it got good feedback, run said feedback through the updatePosition function.
So all of this works SPLENDIDLY if I'm running on a webpage on the same machine. If I move to another machine -- in this case, specifically, an Android phone, doing tethered IP over USB, it ... sort of works. This is what's puzzling me -- it doesn't silently fail, like I'm used to JavaScript doing. Instead, as far as I can tell, it sends the request out, claims that it got data back, and runs the callback function -- only, according to the C program, nothing ever happened; according to Wireshark, no packets were sent; and the "data" that it gets back is an empty, specially-formatted string. If I'm expecting "(5,5)" as output from C, it's getting "(0,0)" -- why is it getting anything at all? Much less filling "(,)" with 0s?
Very strange. I've tried various little things, like changing the dataType to "json", making sure all my permissions are set up right, etc. I'm fairly sure that Apache isn't interfering, since there's nothing in the logs -- what gets me is that I don't see anything on Wireshark, of all things. It's like the phone just plain isn't trying to make the request.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许它正在缓存ajax请求?尝试在 jquery ajax 请求中设置 cache:false 。
maybe it is caching the ajax requests? try setting cache:false in your jquery ajax requests.
你检查过 firebug 或 JS 调试器中的控制台吗?我遇到了这样的问题:由于安全问题,jquery 阻止您以这种方式进行 AJAX 调用,并将返回此错误。
但是,看起来有些人已经尝试解决方法。如果您还没有http:// usejquery.com/posts/9/the-jquery-cross-domain-ajax-guide
Have you checked the console in firebug or a JS debugger? I ran into the issue that jquery prevents you from doing AJAX calls in this manner due to security issues and will return this error
However, it looks like some people have attempted work arounds. Give this a read through if you haven't already http://usejquery.com/posts/9/the-jquery-cross-domain-ajax-guide