简体   繁体   English

为什么异步Javascript获取请求无响应?

[英]Why Asynchronous Javascript Get Request No Response?

i'm having problem to use HTTP.Get request on Javascript, here is my code 我在使用HTTP.Get请求有关Javascript时遇到问题,这是我的代码

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." “ XMLHttpRequest无法加载http://www.example.com/data.php 。所请求的资源上没有'Access-Control-Allow-Origin'标头。”

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. 您为什么不在您的域中创建/托管您的服务,然后在服务器端将HttpRequest发送到example.com/data.php并为您提供所需的响应。

Your page > Gives ajax call to your service > Gives http get call to data.php. 您的页面>调用服务的Ajax>调用http调用data.php。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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