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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:05:18  来源:igfitidea点击:

XMLHttpRequest cannot load.?

javascriptjqueryxml

提问by Kate Thompson

I by chrome->Inspect element->consoleget 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

one work around is using Korzwhich routes all cross origin requests through a third party and sets Access-Control-Allow-Origin headerto '*' so the request goes through.

一种解决方法是使用Korz,它通过第三方路由所有跨源请求并设置Access-Control-Allow-Origin header为“*”,以便请求通过。

回答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