跨网域AJAX请求在Android手机的本机浏览器上表现异常

问题描述:

我的设置如下:


  • SQLite数据库位于我的Apache文件夹之一

  • Perl cgi脚本位于同一文件夹中,具有公共权限,以便它可以处理数据库交互

  • AC程序,在同一台机器上运行,侦听特定端口JSON请求并以特殊格式的JSON数据进行响应

主要使用jQuery和HTML编写的网页必须访问SQLite数据库和C程序有些不断。它使用AJAX请求 - 这是一个例子从代码:

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);
    }
});
}


$ b

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.

所有这项工作SPLENDIDLY如果我在同一台机器上的网页上运行。如果我移动到另一台机器 - 在这种情况下,具体来说,一个Android手机,做网络IP over USB,它...一些工作。这是什么令我困惑的 - 它不会默默失败,像我习惯于JavaScript做。相反,就我所知,它发送请求,声称它获得数据,并运行回调函数 - 只有,根据C程序,没有发生任何事;根据Wireshark,没有发送数据包;并且它获取的数据是一个空的,特殊格式的字符串。如果我期望(5,5)作为C的输出,它得到(0,0) - 为什么它得到任何东西?很少填充(,)与0s?

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?

很奇怪。我已经尝试了各种小事情,如更改dataType为json,确保我的所有权限设置正确等。我相当确定Apache不干扰,因为没有什么在日志中 - 什么得到我是我在Wireshark上看不到任何东西。

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.

也许是缓存的ajax请求?尝试在你的jquery ajax请求中设置cache:false。

maybe it is caching the ajax requests? try setting cache:false in your jquery ajax requests.