简体   繁体   中英

Vanilla JS: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access

I'm trying to do a pretty simple request to another server. I see the expected response in inspection panel of chrome but the browser doesn't allow the request:

XMLHttpRequest cannot load http://search.maven.org/solrsearch/select?q=g%3A%22org.testng%22%20AND%20a%3A%22testng%22&rows=20&wt=json . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

My file is just:

<!DOCTYPE html>
<html>
  <body>

    <div id="testng-version"></div>

    <script type="text/javascript">
    //<![CDATA[

    var xhr = new XMLHttpRequest();
    var url = "http://search.maven.org/solrsearch/select?q=g%3A%22org.testng%22%20AND%20a%3A%22testng%22&rows=20&wt=json";

    xhr.open("GET", url, true);
    xhr.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        var resp = JSON.parse(this.responseText);
        document.getElementById("testng-version").innerHTML = resp.docs[0].latestVersion;
      }
    };

    xhr.send();

    //]]>
    </script>
  </body>
</html>

Browsing the web or similar questions on StackOverflow didn't help.

You need to setup (and learn about) CORS .

To expand a bit, the site you're attempting to access must send CORS headers. Since that's out of your control, what you can do is proxy the site on your web server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question AngularJS : No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access Golang No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'file://' is therefore not allowed access. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '…' is therefore not allowed access Javascript: “ No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. ” Unable to handle scenario: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access XMLHttpRequest cannot load. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin is therefore not allowed access No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4400' is therefore not allowed access
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM