【发布时间】:2016-01-12 03:23:01
【问题描述】:
我正在尝试通过 JS 使用简单的 get 请求连接到谷歌,但它似乎总是给我同样的错误。
“无法在 'XMLHttpRequest' 上执行 'send':无法加载 'http://google.com'。”
任何线索为什么会发生这种情况?相关代码如下。
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);
【问题讨论】:
-
试试“www”,我的意思是
http://www.google.com -
如果您异步运行请求并查看控制台,您会看到更有用的错误消息:
XMLHttpRequest cannot load http://google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. -
提琴手是你的朋友。它甚至向远程服务器发出请求吗?
-
@SudeepJuvekar 你的小提琴获取资源
https://fiddle.jshell.net/_display/www.google.com,而不是http://www.google.com
标签: javascript http