Cross-domain AJAX request behaving oddly on Android phone's native browser
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 no开发者_JAVA技巧thing 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.
maybe it is caching the ajax requests? try setting cache:false in your jquery ajax requests.
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
Access to restricted URI denied" code: "1012
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
精彩评论