简体   繁体   English

XMLHttpRequest异步不起作用xmlhttp.status == 0

[英]XMLHttpRequest asynchronous not working xmlhttp.status== 0

I am working on a project in which i need to fetch data from php page and display it on the server . 我正在一个项目中,我需要从php页面获取数据并将其显示在服务器上。

     var xmlhttp;
                     var jsonResponse="";
                    window.onload=function()
                    { 
                        xmlhttp = new XMLHttpRequest();
                        xmlhttp.open("GET","http://www.ilexsquare.com/EC/menu.php",true);
                        xmlhttp.send();
                        xmlhttp.onreadystatechange=dataReturn;
                    }
                    function dataReturn()
                    {

                        if(xmlhttp.readyState== 4 && xmlhttp.status==200)
                        {
                             jsonResponse = xmlhttp.responseText;
                           getData();
                        }

                    }
                    function getData()
                    {                 
                    json = jsonResponse;

    console.log(json);
    var jsobject = JSON.parse(json);
    console.log(jsobject);
       console.log(jsobject.products.length);
       var html="";
    for(var i = 0; i < jsobject.products.length; i++){ 
 html += '<a class="submenu-item" href="http://www.ilexsquare.com/EC/productpage/index-left1.html?id=' + jsobject.products[i].menuid + '" >'; 
        html += '<i class="fa fa-angle-right"></i><em>  ' + jsobject.products[i].name;     
        html += '</em><i class="fa fa-circle"></i></a>';

    }
          $("#submenu").html(html);
          $("#count").html(jsobject.products.length);
         jsonString = JSON.stringify(obj);
                    }

where is my php page is returning data in json format .in this xmlhttp.status is always returning 0 . 我的php页面在哪里以json格式返回数据。在此xmlhttp.status中始终返回0。 i dont know what is the issue .. please help 我不知道是什么问题..请帮助

This is related to Cross Origin Policy. 这与跨源策略有关。

Consider the following: 考虑以下:

xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","http://www.ilexsquare.com/EC/menu.php",true);
xmlhttp.send();

XMLHttpRequest cannot load http://www.ilexsquare.com/EC/menu.php . XMLHttpRequest无法加载http://www.ilexsquare.com/EC/menu.php No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' http://stackoverflow.com ' is therefore not allowed access. 因此,不允许访问源“ http://stackoverflow.com ”。

There is a great resources on enabling this on your server if you want to use CORS at http://enable-cors.org/server.html 如果您想在http://enable-cors.org/server.html上使用CORS,可以在服务器上启用此功能方面有很多资源。

The header Access-Control-Allow-Origin: * needs to be sent back as a response. 标头Access-Control-Allow-Origin: *需要作为响应发送回去。

This could be as simple as adding header("Access-Control-Allow-Origin: *"); 这可以像添加header("Access-Control-Allow-Origin: *");一样简单header("Access-Control-Allow-Origin: *"); to the top of that PHP page. 到该PHP页面的顶部。

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

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