简体   繁体   English

jQuery Ajax 调用运行出错,但萤火虫显示 200 ok 并返回 json

[英]jQuery Ajax call runs in error but firebug shows 200 ok and returned json

I've read a lot of threads about ajax problems, but I've still got no clue, what could cause my problem:我已经阅读了很多关于 ajax 问题的帖子,但我仍然不知道是什么导致了我的问题:

I'm trying to call a RESTful service (internal) via ajax to get a large json-String.我正在尝试通过 ajax 调用 RESTful 服务(内部)来获取一个大的 json-String。

EDIT: This is how I resolved the problem for now - I know that this is not quite a good solution, but since I can't change anything on the webservice for now this is what I use:编辑:这就是我现在解决问题的方式 - 我知道这不是一个很好的解决方案,但由于我现在无法更改网络服务上的任何内容,这就是我使用的:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
jQuery(document).ready(function(){
    jQuery.support.cors = true;
    jQuery.ajax({
        url: "<internalPage>",
        dataType: 'json'
    })
    .error(function(xhr, err, status) {
        console.log(xhr + err + status);
    })
    .then(function(data) {
        alert("success for me");
        $.each(data.portalServices, function(index, value) {
            $("#someId").append('<li>' + value.name + '</li>');
        });
    });
});

</script>
</head>
<body>
<ul id="someId"></ul>
</body>
</html>

EDIT: This only works in IE not in FF.编辑:这仅适用于 IE,不适用于 FF。

This is what the alert says: Ajax Error Type: GET Requesting Page: Status: 0 - error Error Thrown:这是警报所说的内容:Ajax 错误类型:GET 请求页面:状态:0 - 抛出错误错误:

But in Firebug:但是在 Firebug 中:

Response-Header
Content-Type    application/json
Date    Thu, 06 Mar 2014 07:44:32 GMT
Server  Apache-Coyote/1.1
Transfer-Encoding   chunked
Anfrage-HeaderQuelltext anzeigen
Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Cache-Control   no-cache
Connection  keep-alive
Host    xxxxxx
Origin  null
Pragma  no-cache
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0

Responce-Header from Cache
Content-Type    application/json
Date    Thu, 06 Mar 2014 07:44:32 GMT
Server  Apache-Coyote/1.1
Transfer-Encoding   chunked

Status is 200 OK Response is a valid Json (Really, I've validated it online) {"portalServices":[{"articleID":"ABR_1043024010","name":"SK04 Managed Server: Citrix Basisservice","portalname":"zentrale Arbeitsplatz-Umgebung (Citrix-Basis)","preis":18.0000,"beschreibung":"Mit diesem Produkt stellen wir Ihnen eine zentrale Arbeitsplatz-Umgebung (auf der Basis von Citrix) bereit, die folgende Komponenten ....状态为 200 OK 响应是一个有效的 Json(真的,我已经在线验证过){"portalServices":[{"articleID":"ABR_1043024010","name":"SK04 托管服务器:Citrix Basisservice","portalname" :"zentrale Arbeitsplatz-Umgebung (Citrix-Basis)","preis":18.0000,"beschreibung":"Mit diesem Produkt stellen wir Ihnen eine zentrale Arbeitsplatz-Umgebung (auf der Basis von dienponent) ... .

But there is no JSON tab.但是没有 JSON 选项卡。

When I use cache false Firebug does't show me a response and the error shown in the alert is the following:当我使用缓存 false 时,Firebug 没有显示响应,警报中显示的错误如下:

Ajax Error
Type:GET
Requesting Page: <internalPage>
Status: 0 - Exception ..."Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location "JS frame :: <link to jquery> :: .send :: line 6 " data: no .....

I also tried an ajax call to another service ( " http://rest-service.guides.spring.io/greeting ") which succeeded.我还尝试了对另一个成功的服务(“ http://rest-service.guides.spring.io/greeting ”)的ajax调用。 And there was a JSON tab in firebug, but thats maybe because the json returned from this site is not a list of objects like the json returned from my internal sites webservice. firebug 中有一个 JSON 选项卡,但这可能是因为从该站点返回的 json 不是像从我的内部站点 web 服务返回的 json 那样的对象列表。

Any ideas?有任何想法吗? I think there might be a problem with the webservice, but it would be nice if you got some clues what the cause may be.我认为网络服务可能存在问题,但如果您能了解原因可能是什么,那就太好了。 Thanks a lot.非常感谢。

Even with the following it's still not working =/即使有以下内容,它仍然无法正常工作 =/

headers: {'Access-Control-Allow-Origin': '<internalPage>'},
dataType: 'jsonp',
crossDomain: true

The internalPage is not on the same domain, but would not response to request from an external network. internalPage 不在同一个域中,但不会响应来自外部网络的请求。 The call to the spring services did work without explicitly allowing crossDomain or setting the header like above.在没有明确允许 crossDomain 或像上面那样设置标头的情况下,对 spring 服务的调用确实有效。

I will try as suggested here parsererror after jQuery.ajax request with jsonp content type and come back to tell if it has worked. 在 jQuery.ajax 请求使用 jsonp 内容类型之后,我将按照这里的建议尝试解析器错误,然后回来告诉它是否有效。

EDIT: The WebServices will be changed in future so that I can use jsonp instead of forcing jQuery to allow crossDomain Ajax-Calls.编辑:WebServices 将在未来更改,以便我可以使用 jsonp 而不是强制 jQuery 允许跨域 Ajax 调用。

EDIT: The WebService has been changed, so now it works fine with datatype jsonp.编辑:WebService 已更改,因此现在它可以与数据类型 jsonp 一起正常工作。 =) =)

Thx for your help.谢谢你的帮助。

You sure that this request is causing the error?你确定是这个请求导致了错误? $(document).ajaxError is a global error handler (called on any error), so may be you have a different ajax call that's causing it $(document).ajaxError是一个全局错误处理程序(调用任何错误),所以可能你有一个不同的 ajax 调用导致它

You can change you code to:您可以将代码更改为:

 jQuery.ajax({
        url: "http://<internalPage",
        dataType: 'json'
    })
    .error(function(xhr, err, status) {
       console.log(xhr + err + status);
    })
    .then(function(data) {
       alert("success for me");
    });

Try a different browser ie Chrome, may be you have some extension that's causing the problem尝试不同的浏览器,即 Chrome,可能是您有一些导致问题的扩展程序

Are you using the same url ie not making cross-domain requests?您是否使用相同的网址,即不进行跨域请求? jQuery Call to WebService returns "No Transport" error 对 WebService 的 jQuery 调用返回“无传输”错误

I think your jquery Ajax code has a problem我觉得你的 jquery Ajax 代码有问题

try this code试试这个代码

$.ajax({
    url: "http://<internalPage",
    dataType: 'json',
    success:function(data){
       alert(data); 
       // beter to use eval instead of alert
    }

});

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

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