简体   繁体   中英

Why Asynchronous Javascript Get Request No Response?

i'm having problem to use HTTP.Get request on Javascript, here is my code

function getData(url, callback) {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                callback(this.responseText);
            }
        };

        xhttp.open("GET", url, true);
        xhttp.send();
    }
}

why no response when i call it

getData("http://www.example.com/data.php",   function(data) {            
     console.log(data);
});

Can you please help me? any hint would be very helpful

It is surely because browser prevented cross domain call.

"XMLHttpRequest cannot load http://www.example.com/data.php . No 'Access-Control-Allow-Origin' header is present on the requested resource."

Adding headers to request will not work until the Server or Site honours this header.

Why don't you create/host your on service in your domain which then on server side makes HttpRequest to example.com/data.php and provide you the desired response.

Your page > Gives ajax call to your service > Gives http get call to data.php.

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