无法在'XMLHttpRequest'Syncronous上执行'send' [英] Failed to execute 'send' on 'XMLHttpRequest' Syncronous
问题描述
我正在尝试通过JS通过简单的get请求连接谷歌,而且似乎总是给我同样的错误。
I'm trying to connect to google with a simple get request through JS and it seems to always be giving me the same error.
执行失败发送'on'XMLHttpRequest':无法加载' http://google.com '。
"Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://google.com'."
任何线索为什么会发生这种情况?相关代码如下。
Any clue why this would be happening? Relevant code is below.
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "http://google.com", false);
try {
xmlHttp.send();
} catch (err) {
alert("EXCEPTION: " + err.message);
}
alert("here's the result of the get: " + xmlHttp.responseText);
推荐答案
这只是一个跨域权限失败,到期到同源政策。如果您异步运行同一个请求并在控制台中查看,则会看到更有用的错误消息:
This is simply a cross-origin permission failure, due to the same origin policy. If you ran this same request asynchronously and looked in your console, you'd see the much more helpful error message:
XMLHttpRequest无法加载< a href =http://google.com/ =nofollow> http://google.com/ 。请求的资源上不存在Access-Control-Allow-Origin标头。因此,不允许访问来源' http:// [whatever] 。
XMLHttpRequest cannot load http://google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://[whatever]' is therefore not allowed access.
这是因为只有在 http://www.google.com
的网页上运行的脚本才能从<$ c中读取资源$ C> http://www.google.com 。如果正在获取的资源提供了适当的CORS头(例如, Access-Control-Allow-Origin
),则不会看到此错误。 (但是, http://www.google.com
不提供此类标题。)
This is because only scripts run on pages from http://www.google.com
may read resources from http://www.google.com
. If the resource being fetched served appropriate CORS headers (e.g., Access-Control-Allow-Origin
), you would not see this error. (However, http://www.google.com
serves no such headers).
这篇关于无法在'XMLHttpRequest'Syncronous上执行'send'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!