简体   繁体   English

使用 jquery 读取 json 数据时出现问题。

[英]Problems reading json data with jquery.

I am having problems using jquery to grab json data from a web service that lies on a different subdomain from where my client side code is.我在使用 jquery 从位于与我的客户端代码所在的不同子域的 web 服务中获取 json 服务时遇到问题。 When I access the exact same json data from a local text file, my code works fine.当我从本地文本文件访问完全相同的 json 数据时,我的代码工作正常。

The json data is coming from this address json 数据来自此地址

var jsonFeed = https://crm.bmw.ca/webservices/RetailerLocator.ashx?language=en&callback=?

The MIME type of the data is text/html, however I have also tried application/json.数据的 MIME 类型是 text/html,但是我也尝试过 application/json。

Here is one method of access这是一种访问方法

$.getJSON(jsonFeed, function (data) {
    $.each(data, function (i, item) {
        alert(item);
    });
});

I've also tried this method, which came back with a parsererror.我也尝试过这种方法,它返回了一个解析器错误。 I've also tried this with a jsonp datatype我也尝试过使用 jsonp 数据类型

$.ajax(jsonFeed, {
    crossDomain: true,
    dataType: "json",
    success: function (data, text) {
        $.each(data, function (i, item) {
            alert(item);
        });
    },
    error: function (request, status, error) {
        alert(status + ", " + error);
    }
});

My code has to be entirely client side so a proxy isn't an option right now.我的代码必须完全是客户端,所以现在不能选择代理。

An example of someone with a very similar problem can be found here.可以在此处找到具有非常相似问题的人的示例。 jQuery AJAX JSON dataType Conversion jQuery AJAX JSON数据类型转换

You can only work within the confines of what is possible.你只能在可能的范围内工作。 Same-origin policy can't be subverted, although you can use things like cross-domain policy headers on each of your servers to essentially link them together.同源策略不能被破坏,尽管您可以在每台服务器上使用跨域策略标头之类的东西将它们基本上链接在一起。 However, that's only supported in the newer crop of browsers, and you have to control all the servers in the network.但是,这仅在较新的浏览器中受支持,并且您必须控制网络中的所有服务器。

See: http://en.wikipedia.org/wiki/Same_origin_policy for more information on what you're up against.请参阅: http://en.wikipedia.org/wiki/Same_origin_policy ,了解有关您所面临的问题的更多信息。

So far as I can tell from playing with JSFiddle (http://jsfiddle.net/CEDB5/), the question/answer you mentioned is correct: unless crm.bmw.ca starts sending the correct MIME type you are stuck.据我从玩 JSFiddle (http://jsfiddle.net/CEDB5/) 中可以看出,你提到的问题/答案是正确的:除非 crm.bmw.ca 开始发送正确的 MIME 类型,否则你会被卡住。

While the returned JSON data should probably be of type text/json, the bigger problem is that the API call is not respecting your "callback" parameter.虽然返回的 JSON 数据可能应该是 text/json 类型,但更大的问题是 API 调用不尊重您的“回调”参数。 Since you're calling the API cross-domain you have to use JSONP which means your data should be returned inside of a function call.由于您调用的是 API 跨域,因此您必须使用 JSONP,这意味着您的数据应在 function 调用中返回。 For example, if you navigate to https://crm.bmw.ca/webservices/RetailerLocator.ashx?language=en&callback= mycallback you should see something like this returned:例如,如果您导航到https://crm.bmw.ca/webservices/RetailerLocator.ashx?language=en&callback= mycallback ,您应该会看到返回如下内容:

mycallback( [{"RetailerID":1110,"Name":"BMW St. John's","Address":"120 Kenmount Road"... ) mycallback( [{"RetailerID":1110,"Name":"BMW St. John's","Address":"120 Kenmount Road"... )

The fact that the callback function name specified in the "callback" argument isn't showing up as part of the returned data probably means that you're using an incorrect name for that parameter. “回调”参数中指定的回调 function 名称未作为返回数据的一部分显示的事实可能意味着您为该参数使用了不正确的名称。 Or, it could be that the system is not configured to allow cross-domain requests.或者,可能是系统未配置为允许跨域请求。 You should contact the system admin and make sure the API allows cross-domain requests and also check the docs for that API and make sure you're using the correct callback parameter name.您应该联系系统管理员并确保 API 允许跨域请求,并检查该 API 的文档并确保您使用正确的回调参数名称。

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

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