简体   繁体   English

getJSON返回200但出现错误

[英]getJSON returns 200 but errors

I am trying to write some simple Javascript to load a JSON payload from a URL. 我正在尝试编写一些简单的Javascript以从URL加载JSON负载。

I know I have CORS issues, and for the moment I'm fine with that. 我知道我有CORS问题,目前我对此表示满意。 I can disable chrome's check. 我可以禁用Chrome的检查。

The problem I have is that I get back a 200 OK response, but also an error and I don't understand why. 我遇到的问题是我得到200 OK响应,但同时还是一个错误,我不明白为什么。

 var urlAddress = "https://light-ratio-149809.appspot.com/ESI-OrgUnitService?jsoncallback=?"; $.getJSON(urlAddress, { format: "json" }) .always(function( jqXHR, textStatus, errorThrown ) { console.log("reponse textStatus["+textStatus+"] errorThrown["+errorThrown+"] [" + jqXHR.responseText + "]"); }) .done(function(data) { console.log("success"); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

Can someone please tell me where I'm going wrong? 有人可以告诉我我要去哪里了吗?

If I use this JSON payload, it works. 如果我使用此JSON有效负载,则可以正常工作。

https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json

The error looks to be a parse error. 该错误看起来是解析错误。

reponse textStatus[parsererror] errorThrown[Error: jQuery331004379421802427674_1551456238952 was not called] [undefined] 响应textStatus [parsererror] errorThrown [错误:未调用jQuery331004379421802427674_1551456238952] [未定义]

The parseError is because jQuery thinks you're expecting a JSONP response (ie some executable Javascript), not just JSON data. parseError是因为jQuery认为您期望的是JSONP响应(即某些可执行Javascript),而不仅仅是JSON数据。 It thinks that because you added the jsonCallback querystring parameter to the URL. 它认为是因为您将jsonCallback querystring参数添加到了URL。

Once you remove that, it goes back to expecting regular JSON (which is what the endpoint returns), but is then defeated by the server's CORS restrictions. 删除它后,它可以返回到期望的常规JSON(端点返回的内容),但是随后由于服务器的CORS限制而失败。

You can't get round CORS by pretending you want to receive JSONP when that's not actually what the server is returning. 假装您不想接收服务器返回的JSONP,就无法获得圆满的CORS。 I suggest you find out whether this server does in fact have a JSONP endpoint, or whether your app can be added as an allowed CORS client. 我建议您找出此服务器是否确实具有JSONP端点,或者是否可以将您的应用程序添加为允许的CORS客户端。

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

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