1

我只是新手,从一些教程中学习 AJAX。视频结束后,当我加载教程中给出的文件(不做任何更改)时,我得到了 xml 状态 0,以及一些控制台错误。这是代码

window.onload = makeRequest;
var xhr = false;

function makeRequest() {
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else {
        if (window.ActiveXObject) {
            try {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) { }
        }
    }

    if (xhr) {
        xhr.onreadystatechange = showState;
        xhr.open("GET", "colors.xml", true);
        xhr.send(null);
    }
    else {
        document.getElementById("updateArea").innerHTML = "Sorry, but I couldn't create an XMLHttpRequest";
    }
}

function showState() {
    var currMsg = document.getElementById("updateArea").innerHTML;
    document.getElementById("updateArea").innerHTML = currMsg + "<p>The current state is " + xhr.readyState + " and the status is " + xhr.status + "</p>";
}
4

0 回答 0