I'm using jQuery to make an AJAX call to a remote server, but with the code as it is (and ostensibly correct) I get an empty response. If I开发者_如何学Go change the dataType to "script," I get the expected XML in the response, but I'm unable to do anything with it. Here's some pseudocode I'm working with:
function sendData(data) { $.ajax( { type: "GET", url: "remote_server", dataType: "xml", data: "parameter=" + data, complete: function(xml) { console.info('XML result is',xml); }, contentType: "text/xml; charset=utf-8" }); }
A few additional things to note are that I'm using a local .htm file to call the remote server. When I use a Web browser (http://remote_server/page?parameter=value), I get a valid XML response. Finally, the XML response header has encoding type of ASCII, though I've also tried changing the charset value in my code to ASCII with the same result.
I appreciate any help you can provide.
The problem you are running into is the same origin policy. You can't make an AJAX request, unless you use JSONP, to a URL in another domain. JSONP gets around this by loading up a script tag with your URL as the src and having the web server respond with a bit of code wrapped around the JSON result that executes a callback to your javascript method.
You can use the NET tab in firebug to see what call is being made and what the actual response is. This can help a LOT.
You can't make an AJAX request to a remote domain. What you can do, is create a script-include, but it requires that the response is being sent as a javascript function call:
callback('<xml/>');
instead of just
<xml/>
Just a hunch, but check the mime-type of the data being returned via the ajax function. If you request xml, the script is expecting content encoded as text/xml or some other derivative.
You may also be running into the same origin policy, in which case you'll need to refactor everything to json and leverage jsonp.
精彩评论