繁体   English   中英

AJAX w/jQuery 没有从请求中得到预期的(或任何)响应

[英]AJAX w/jQuery not getting expected (or any) response from a request

我没有从我通过 jQuery.ajax ZDB97423871043ADE6 发出的 web 服务请求中得到预期的响应 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).

我正在对我的 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);
    }
});

在发出请求后,我总是收到错误 function 调用,准备好 state == 4 和状态 == 0。

但是,如果我将 dataType 更改为“text/xml”或“text”,那么我将进入成功 function,但传递给该方法的响应 XML 数据为空(jqXHR ZA8CFDE6331BD59B4666696 的 responseXML 属性为 null)9111BD59B466696

发出请求后,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

它在 Response 选项卡下没有显示任何内容(表示空有效负载?),在 XML 选项卡下您会看到:

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

^

似乎我在 jQuery.ajax() 调用中做错了,这阻止了 XML 有效负载被包含在响应中。

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

在此先感谢您的帮助。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM