admin管理员组文章数量:1391771
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.
"Failed to execute 'send' on 'XMLHttpRequest': Failed to load ''."
Any clue why this would be happening? Relevant code is below.
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "", false);
try {
xmlHttp.send();
} catch (err) {
alert("EXCEPTION: " + err.message);
}
alert("here's the result of the get: " + xmlHttp.responseText);
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.
"Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://google.'."
Any clue why this would be happening? Relevant code is below.
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "http://google.", false);
try {
xmlHttp.send();
} catch (err) {
alert("EXCEPTION: " + err.message);
}
alert("here's the result of the get: " + xmlHttp.responseText);
Share
Improve this question
asked Jan 11, 2016 at 19:03
CoatCoat
7179 silver badges18 bronze badges
7
-
Try with "www", I mean
http://www.google.
– Mathew B. Commented Jan 11, 2016 at 19:09 -
If you run the request asynchronously and look in your console, you'll see a much more helpful error message:
XMLHttpRequest cannot load http://google./. No 'Access-Control-Allow-Origin' header is present on the requested resource.
– apsillers Commented Jan 11, 2016 at 19:10 - 1 Same Origin Policy – epascarello Commented Jan 11, 2016 at 19:11
- Fiddler is your friend. Does it even make a request to the remote server? – Zuzlx Commented Jan 11, 2016 at 19:12
-
2
@SudeepJuvekar Your fiddle fetches the resource
https://fiddle.jshell/_display/www.google.
, rather thanhttp://www.google.
– apsillers Commented Jan 11, 2016 at 19:20
1 Answer
Reset to default 4This 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 cannot load http://google./. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://[whatever]' is therefore not allowed access.
This is because only scripts run on pages from http://www.google.
may read resources from http://www.google.
. 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.
serves no such headers).
本文标签: javascriptFailed to execute 39send39 on 39XMLHttpRequest39 SyncronousStack Overflow
版权声明:本文标题:javascript - Failed to execute 'send' on 'XMLHttpRequest' Syncronous - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744763769a2623908.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论