JSON无法与JQuery一起使用 [英] JSON not working with JQuery

查看:122
本文介绍了JSON无法与JQuery一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JSON是否有问题?

Is there something wrong with my JSON?

http://codepen.io/anon/pen/Ieyvl

var url = "http://x.com/json.js";

$.getJSON(url, function(response){
    alert("worked!");
    alert(response);
});

如果这是一个跨域"问题,那为什么行得通呢?

If its a "cross-domain" issue, then why does this work?

http://codepen.io/anon/pen/BHshC

var url = "http://gdata.youtube.com/feeds/api/playlists/PL3E245DF445E37F50?v=2&alt=jsonc";

$.getJSON(url, function(response){
    alert("worked!");
    alert(response);
});

推荐答案

查看控制台:

XMLHttpRequest无法加载 http://xml.hosting.subsplash.com/5KQ4CM/json.js .起源 不允许 http://secure.codepen.io 访问控制允许来源.

XMLHttpRequest cannot load http://xml.hosting.subsplash.com/5KQ4CM/json.js. Origin http://secure.codepen.io is not allowed by Access-Control-Allow-Origin.

了解相同来源政策.

如果他们支持JSONP,则需要使用它.如果服务器支持,请查看 CORS

You need to use JSONP if they support it. And if the server supports it look into CORS

我没有检查,但是如果Google的示例有效,则可能是因为该资源的Google启用了CORS.这意味着支持它的浏览器可以通过Ajax调用来请求资源.并非所有浏览器都支持该类型的握手请求. [看着您的旧版IE]

I did not check, but if the Google example works, it is probably because Google Enabled CORS for that resource. That mean browsers that support it can request the resource with an Ajax call. Not all browsers support that type of handshake request. [looking at you older IEs]

这篇关于JSON无法与JQuery一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆