I'm not getting the expected response from a web service request that I'm making via the jQuery.ajax API. The web service is getting the request (I can see it being processed when the jQuery.ajax() call is made) and returning the proper XML (I can see the expected XML when I make the GET request from a browser).
I'm making the AJAX call to my web service like so:
$.ajax({
type: "GET",
url: url_to_web_service,
dataType: "xml",
success: function (xml) {
// the array of image source locations we'll build from the XML
var thumbImgArray = new Array();
// find every image source location and add it to the array
$(xml).find("image_src_location").each(function () {
thumbImgArray.push($(this));
});
// update the scrollable thumbnail images using the new array of image source locations
updateScrollableThumbs($(xml).find("indicator"), thumbImgArray);
},
error: function (xhr, err) {
alert("AJAX error function invoked: \n\treadyState: " + xhr.readyState + "\n\tstatus: " + xhr.status);
$('.error').html("responseText: " + xhr.responseText);
}
});
I always get the error function invoked after the request is made, with a ready state == 4 and the status == 0.
However if I change the dataType to "text/xml" or "text" then I will get into the success function, but the response XML data passed into the method is empty (the responseXML property of the jqXHR object is null).
Once the request is made Firebug shows the below:
Response Headers
Server Apache-Coyote/1.1
Content-Type text/xml;charset=ISO-8859-1
Content-Language en-US
Content-Length 1326
Date Wed, 25 May 2011 16:02:19 GMT
Request Headers
Host localhost:8080
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110420 Firefox/3.6.17 ( .NET CLR 3.5.30729)
Accept application/xml, text/xml, */*
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Origin null
It shows nothing under the Response tab (indicative of an empty payload?), and under the XML tab you see this:
XML Parsing Error: no element found Location: moz-nullprincipal:{a6fb7963-9b10-464c-a07d-c8e439b98f0d} Line Number 1, Column 1:
开发者_JS百科
^
It seems that I'm doing something wrong in the jQuery.ajax() call which is preventing the XML payload from being included as part of the response.
Can anyone suggest where I should look for the error?
Thanks in advance for your help.
It looks like I was violating the same origin policy after all. The code worked as expected once I placed the HTML file on the same Tomcat server with the web service. In other words I assumed that file://path/to/my/htmlfile and http://localhost:8080/my/web/service would appear to be the same domain, but apparently that's not how it works.
I suggest that you try using fiddler or similar tool, so you can capture your request and response. After installing it you will get network tab in IE developer tools where you can capture and inspect network traffic, and see response from server after jquery makes request. Under response body you will see what server sent in response, and probably find out what the problem is.
I believe that your XML is malformed. Perhaps you have a space or line at the beginning or end of the XML document. For that reason, when you change to text, you are able to get a successful response. Also, you are getting this error: "XML Parsing Error: no element found Location: moz-nullprincipal:{a6fb7963-9b10-464c-a07d-c8e439b98f0d} Line Number 1, Column 1:" which is telling you that there is something wrong with the XML and the browser is unable to recognize the response as XML
精彩评论