使用$ .getJSON时出现错误"Access-Control-Allow-Origin不允许访问源" [英] Error 'Origin is not allowed by Access-Control-Allow-Origin' while using $.getJSON
问题描述
我正在使用jquery $.getJSON
,并不断出现以下错误.
I am using jquery $.getJSON
and keep getting the following error.
来源 http://example.co.uk 不允许 访问控制允许来源.
Origin http://example.co.uk is not allowed by Access-Control-Allow-Origin.
我遇到的问题是它工作正常,然后突然开始执行此操作的任何原因,这就是我的代码.
The issue i am having is it was working fine then suddenly started to do this any reason why this might happen here is my code.
http://jsfiddle.net/isimpledesign/KnUBH/
推荐答案
当您尝试从您的站点向另一个域(或子域)上的站点发出请求时,会出现此错误.尽管可以配置其他站点以允许您发出请求(请参阅Mozilla文档),并非所有地方都支持此功能.
You get this error when you try to make a request from your site to a site on a different domain (or sub-domain). It is possible to configure the other site to allow you to make requests (see Mozilla documentation), although this feature is not supported everywhere.
JSONP可以替代普通的AJAX调用,它需要服务器支持它,但没有跨域限制.事实证明,Blogger确实支持此功能,因此您只需将URL修改为
An alternative to normal AJAX calls is JSONP, which requires the server to support it but doesn't have cross-domain restrictions. It turns out that Blogger does support this, so you just need to modify the URL to
var url = 'http://isimpledesign.blogspot.com/feeds/posts/default?alt=json&callback=?'
,jQuery将正确处理它. (小提琴)
and jQuery will handle it properly. (Fiddle)
这篇关于使用$ .getJSON时出现错误"Access-Control-Allow-Origin不允许访问源"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!