简体   繁体   中英

Origin null is not allowed by Access-Control-Allow-Origin (Synchronous, no jQuery)

The first thing I would like to say is that I looked around for an answer to this for quite some time, but everything I found was about jQuery. This is not about jQuery.

I have some code (below), but when I ran it in Firefox then Firebug gave me this big, incomprehensible exception. I tried it in Google Chrome and got something a bit more useful: "XMLHttpRequest cannot load http://www.wikipedia.org/ . Origin null is not allowed by Access-Control-Allow-Origin." (If you'd like to know why I was trying to access Wikipedia, I often use it as a test site.) Here is my code:

function requestSite(url) {
    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", url, false);
    xmlhttp.send();
    return xmlhttp;
}

All I tried to do with it was access Wikipedia. Then Google, which gave me the same result. Perhaps it is worth noting that when I triggered the function with a button on the page, it gave me this, while using the console was exactly the same, but without the error message. Another thing that may or may not be worth mentioning is that I was running this file from my local filesystem.

You can't make an AJAX request to a URL that is on a different domain. It's a basic browser security issue.

This error message means

"You are running this request from your local system".

Making ajax requests to some other domain besides the one you are running the code is (of course) not allowed.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM