首页 > 如何用jQuery跨越获取json信息

如何用jQuery跨越获取json信息

json的提供地址是: http://contests.acmicpc.info/contests...
我采用下面的代码,会出现不允许跨越的提醒,请问有什么办法吗。

$.getJSON('http://contests.acmicpc.info/contests.json');

在本地网站服务器端抓取异域 json,再输出给前端。


建议使用 CORS (Cross-Origin Resource Sharing) 方案
https://developer.mozilla.org/en-US/d...

JSONP会有被XSS的风险


可以搜索看看jsonp方面的资料


ajax自身有着跨域的限制,虽然现在部分浏览器下面有解决方案,但是还是不能正式使用。
先看下JSONP,http://zh.wikipedia.org/wiki/JSONP
JQUERY的官方文档里面有跨域的例子可以参考,http://api.jquery.com/jQuery.getJSON/。留意下获取的不是ajax常用的json,而应该是动态生成的js文件。
祝好运。


借用 @AlvinTin 答案的里链接, 楼主看下里面 JSONP 的写法:
jQuery 在存在 callback=? 的 URL 时会自动启用 JSONP, 一般 GET 请求这都能解决了的
http://api.jquery.com/jQuery.getJSON/...

JSONP

If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details.
【热门文章】
【热门文章】