使用 jquery 读取 json 数据时出现问题。
我在使用 jquery 从 Web 服务获取 json 数据时遇到问题,该服务位于与我的客户端代码不同的子域中。当我从本地文本文件访问完全相同的 json 数据时,我的代码工作正常。
json数据来自这个地址
var jsonFeed = https://crm.bmw.ca/webservices/RetailerLocator.ashx?language=en&callback=?
数据的MIME类型是text/html,但是我也尝试过application/json。
这是一种访问方法,
$.getJSON(jsonFeed, function (data) {
$.each(data, function (i, item) {
alert(item);
});
});
我也尝试过这种方法,但返回了解析器错误。我还尝试过使用 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);
}
});
我的代码必须完全是客户端的,因此代理现在不是一个选项。
可以在此处找到具有非常相似问题的人的示例。 jQuery AJAX JSON 数据类型转换
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. When I access the exact same json data from a local text file, my code works fine.
The json data is coming from this address
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.
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
$.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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你只能在可能的范围内工作。尽管您可以在每个服务器上使用跨域策略标头之类的东西将它们本质上链接在一起,但同源策略无法被破坏。但是,这仅在较新的浏览器中受支持,并且您必须控制网络中的所有服务器。
请参阅:http://en.wikipedia.org/wiki/Same_origin_policy 了解有关您的内容的更多信息正在对抗。
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.
虽然返回的 JSON 数据可能应该是 text/json 类型,但更大的问题是 API 调用不尊重您的“回调”参数。由于您要跨域调用 API,因此必须使用 JSONP,这意味着您的数据应在函数调用内返回。例如,如果您导航到 https://crm.bmw.ca /webservices/RetailerLocator.ashx?language=en&callback=mycallback 您应该看到类似这样的返回内容:
mycallback([{"RetailerID":1110,"Name":"BMW St. John's","Address":"120 Kenmount Road"...)
事实“callback”参数中指定的回调函数名称未显示为返回数据的一部分,这可能意味着您对该参数使用了不正确的名称。或者,系统可能未配置为允许跨域请求。您应该联系系统管理员并确保 API 允许跨域请求,并检查该 API 的文档并确保您使用正确的回调参数名称。
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. 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. For example, if you navigate to https://crm.bmw.ca/webservices/RetailerLocator.ashx?language=en&callback=mycallback you should see something like this returned:
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. 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.
据我通过使用 JSFiddle (http://jsfiddle.net/CEDB5/) 可以看出,您提到的问题/答案是正确的:除非 crm.bmw.ca 开始发送正确的 MIME 类型,否则您将陷入困境。
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.