XMLHttpRequest readyState停留在1且永远不会达到4

这是我的代码。我不知道我的代码有什么问题,但是readyState始终返回1,而状态始终返回0。有人可以帮助我吗?

function Registered() {
    console.log("masuk function");
    var xmlhttp;
    var randomNum = Math.floor((Math.random() * 100) + 1);
    const proxyurl = "https://cors-anywhere.herokuapp.com/";
    var url = "http://127.0.0.1:8080/webchatBack/webchat/getQueueCount?t="+randomNum;
    var xmlDoc;

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        console.log("masuk xml");
        xmlhttp = new XMLHttpRequest();
    } else if(window.ActiveXObject){// code for IE6, IE5
        console.log("masuk active");
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        console.log("masuk function lagi");
        console.log(xmlhttp.readyState);
        console.log(xmlhttp.status);
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            console.log("masuk if");
            xmlDoc = xmlhttp.responseXML;
            var result = xmlDoc.getElementsByTagName("Result");
            result = result[0].childNodes[0].nodeValue;
            console.log(result);
            document.getElementById("agentLoginData").innerHTML = result;
        }
    }

    xmlhttp.open("GET", url, true);
    xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=\"utf-8\"");
    xmlhttp.send('');
}
评论
  • uaut
    uaut 回复

    You're not using the proxy.

    Just make use of it: concatenating the proxy url will get rid of cros error.

    function Registered() {
        console.log("masuk function");
        var xmlhttp;
        var randomNum = Math.floor((Math.random() * 100) + 1);
        const proxyurl = "https://cors-anywhere.herokuapp.com/";
        var url = proxyurl + "http://127.0.0.1:8080/webchatBack/webchat/getQueueCount?t=" + randomNum; // Notice Here
        var xmlDoc;
    
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            console.log("masuk xml");
            xmlhttp = new XMLHttpRequest();
        } else if (window.ActiveXObject) {// code for IE6, IE5
            console.log("masuk active");
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            console.log("masuk function lagi");
            console.log(xmlhttp.readyState);
            console.log(xmlhttp.status);
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                console.log("masuk if");
                xmlDoc = xmlhttp.responseXML;
                var result = xmlDoc.getElementsByTagName("Result");
                result = result[0].childNodes[0].nodeValue;
                console.log(result);
                document.getElementById("agentLoginData").innerHTML = result;
            }
        };
    
        xmlhttp.open("GET", url, true);
        xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=\"utf-8\"");
        xmlhttp.send('');
    }