Javascript XMLHttpRequest 无法加载。?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7157393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XMLHttpRequest cannot load.?
提问by Kate Thompson
I by chrome->Inspect element->console
get this error:
我chrome->Inspect element->console
得到这个错误:
XMLHttpRequest cannot load. Origin is not allowed by Access-Control-Allow-Origin.
XMLHttpRequest 无法加载。Access-Control-Allow-Origin 不允许 Origin。
What is this resolved?
这是解决了什么问题?
回答by Tigraine
You cannot issue requests through the XMLHttpRequest to other domains or subdomains. If you are issuing the request from www.foo.com you also need to target the request at www.foo.com and not leave out the www.
您不能通过 XMLHttpRequest 向其他域或子域发出请求。如果您是从 www.foo.com 发出请求,您还需要将请求定位到 www.foo.com,而不是遗漏 www.foo.com。
If you really need to hit another domain you can use JsonPwhere the browser utilizes the <script>
tags ability to load scripts from a different domain. The loaded script then executes a callback function to give you the data. But for regular AJAX calls you cannot leave the source domain at all.
如果您确实需要访问另一个域,您可以使用JsonP,其中浏览器利用<script>
标签功能从不同域加载脚本。加载的脚本然后执行回调函数为您提供数据。但是对于常规的 AJAX 调用,您根本无法离开源域。
See the Wiki article on Same Origin Policy
请参阅有关同源政策的 Wiki 文章
回答by yonatan
回答by beitomartinez
I recommend you to read this: http://www.fbloggs.com/2010/07/09/how-to-access-cross-domain-data-with-ajax-using-jsonp-jquery-and-php/
我建议你阅读这个:http: //www.fbloggs.com/2010/07/09/how-to-access-cross-domain-data-with-ajax-using-jsonp-jquery-and-php/
It is very wel explained... the whole point is that you need to return your JSON in a callback-function way
解释得很好......重点是你需要以回调函数的方式返回你的JSON