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 than http://www.google. – apsillers Commented Jan 11, 2016 at 19:20
 |  Show 2 more ments

1 Answer 1

Reset to default 4

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