AJAX w/jQuery 未从请求中获得预期(或任何)响应

发布于 2024-11-09 09:39:05 字数 2050 浏览 0 评论 0原文

我没有从通过 jQuery.ajax API 发出的 Web 服务请求中得到预期的响应。 Web 服务正在获取请求(当调用 jQuery.ajax() 时,我可以看到它正在被处理)并返回正确的 XML(当我从浏览器发出 GET 请求时,我可以看到预期的 XML)。

我正在对我的 Web 服务进行 AJAX 调用,如下所示:

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

我总是在发出请求后调用错误函数,就绪状态 == 4 且状态 == 0。

但是,如果我将 dataType 更改为“text” /xml”或“text”然后我将进入成功函数,但传递到该方法的响应XML数据为空(jqXHR对象的responseXML属性为空)。

发出请求后,Firebug 会显示以下内容:

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

它在“响应”选项卡下不显示任何内容(表明有效负载为空?),在 XML 选项卡下您会看到以下内容:

XML Parsing Error: no element found Location: moz-nullprincipal:{a6fb7963-9b10-464c-a07d-c8e439b98f0d} Line Number 1, Column 1:

^

看来我在 jQuery.ajax( ) 调用会阻止 XML 有效负载作为响应的一部分包含在内。

谁能建议我应该在哪里查找错误?

预先感谢您的帮助。

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:

^

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱给你人给你 2024-11-16 09:39:05

看来我毕竟违反了同源政策。当我将 HTML 文件与 Web 服务放在同一个 Tomcat 服务器上时,代码就按预期工作了。换句话说,我假设 file://path/to/my/htmlfile 和 http://localhost:8080 /my/web/service 看起来是同一个域,但显然这不是它的工作原理。

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.

往日 2024-11-16 09:39:05

我建议您尝试使用 fiddler 或类似的工具,这样您就可以捕获您的请求和响应。安装后,您将在 IE 开发人员工具中获得网络选项卡,您可以在其中捕获和检查网络流量,并在 jquery 发出请求后查看服务器的响应。在响应正文下,您将看到服务器发送的响应内容,并可能找出问题所在。

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 2024-11-16 09:39:05

我认为您的 XML 格式错误。也许 XML 文档的开头或结尾处有空格或行。因此,当您更改为文本时,您可以获得成功的响应。另外,您还会收到以下错误:“XML Parsing Error: no element found Location: moz-nullprincipal:{a6fb7963-9b10-464c-a07d-c8e439b98f0d} Line Number 1, Column 1:”,这告诉您出现了问题使用 XML 并且浏览器无法将响应识别为 XML

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文