简体   繁体   English

为什么我的jQuery Ajax调用不成功

[英]Why my jQuery ajax call is not successful

I am developing my web app and run it on localhost:8080, in my js file, I have an ajax call to get xml data from server: 我正在开发我的Web应用程序并在localhost:8080上运行它,在我的js文件中,我有一个ajax调用来从服务器获取xml数据:

$.ajax({
        url: 'http://COMPANY_DOMAIN.com/company-interface/the-id',
        type: 'GET',
        async: false,
        dataType: 'application/xml',
        data: {id: 43},
        success: function(data) {

            alert(data);

        },
        error: function(xhr, status, error){
            alert('error happens');
        }
     })

I can access the url http://COMPANY_DOMAIN.com/company-interface/the-id/?id=43 which will show the xml result on browswer, but my above ajax call always go to error function. 我可以访问URL http://COMPANY_DOMAIN.com/company-interface/the-id/?id=43 ,它将在浏览器上显示xml结果,但是我上面的ajax调用总是转到错误功能。

I checked in firebug, the 'xml' tab shows " XML Parsing Error: no element found Location: moz-nullprincipal:{9fd0dca8-cf07-4401-b1de-ab04e8aa00bc} Line Number 1, Column 1 :" and firebug shows the URL GET is http://COMPANY_DOMAIN.com/company-interface/the-id/?id=43& =1302610001570 . 我检入了Firebug,“ xml”标签显示“ XML解析错误:找不到元素位置:moz-nullprincipal:{9fd0dca8-cf07-4401-b1de-ab04e8aa00bc}行号1,第1列 :”,firebug显示了URL GET是http://COMPANY_DOMAIN.com/company-interface/the-id/?id=43& = 1302610001570

Why firebug GET shows the " ...& =1302610001570 "? 为什么萤火虫GET显示“ ...&= 1302610001570 ”? what does it means? 这是什么意思? why my ajax call is failed, though I can access that URL? 为什么我可以访问该URL,但我的ajax调用失败?

----------------EDIT--------------- - - - - - - - - 编辑 - - - - - - - -

Hi, I changed to use localhost request like: 嗨,我改为使用本地主机请求,如:

$.ajax({
            url: 'http://localhost:8080/company-interface/the-id',
            type: 'GET',
            async: false,
            dataType: 'xml',
            data: {id: 43},
            success: function(data) {

                alert(data);

            },
            error: function(xhr, status, error){
                alert('error happens');
            }
         })

But I got the same error... more suggestions please... thank you. 但是我遇到了同样的错误...请提供更多建议...谢谢。

This is possibly due to cross domain access control . 这可能是由于跨域访问控制 You are accessing the site which is on your machine which tries to connect to another website. 您正在访问试图连接到另一个网站的计算机上的站点。 This is not allowed unless you define Access-Control-Allow-Origin headers. 除非您定义Access-Control-Allow-Origin标头,否则这是不允许的。

Also as @Craig said, content type as xml is needs to be changed. 就像@Craig所说的那样,需要更改xml内容类型。

Try Below code : 试试下面的代码:

 $.ajax({
            url: 'http://localhost:8080/company-interface/the-id',
            type: 'GET',
            async: false,
            dataType: 'text',
            data: {id: 43},
            success: function(data) {

            // Assume response like..
            // <note>
            //    <from>Jani</from>
            //    <to>Tove</to>
            //    <message>Remember me this weekend</message>
            //  </note>

            xmlDoc = $.parseXML( data ),
            $xml = $( xmlDoc ),
            $message = $xml.find("message")
            alert($message.text());
            $("#xmlResonse").html($message.text());

        },
        error: function(xhr, status, error){
            alert('error happens');
        }
     })

Javascript is subject to the same origin policy . JavaScript遵循相同的原产地政策 Your script running on localhost can't access COMPANY_DOMAIN.com. 您在本地主机上运行的脚本无法访问COMPANY_DOMAIN.com。

change your datatype to application/xml or text/xml 将您的数据类型更改为application/xmltext/xml

As the other comment suggests, is your URL on another domain? 就像其他评论所暗示的那样,您的URL是否在另一个域上? If not then try using relative URL's instead. 如果不是,请尝试使用相对URL。

1302610001570 is for measuring response, 1302610001570用于测量响应,

did you checked if the response create valid xml? 您是否检查了响应是否创建了有效的xml?

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

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