最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - XMLHttpRequest ReadyState always undefined - Stack Overflow

programmeradmin8浏览0评论

I'm following the PHP/AJAX tutorials at w3schools, and I've hit a bit of a roadblock at square one. Every time I call this function, the readystate is always undefined.

function showHint(str) {
    if (str.length == 0) {
        document.getElementById("txtHint").innerHTML = "";
            return;
        }

        var xmlhttp;

        if (window.XMLHttpRequest) {
            console.log("Using XMLHttpRequest");
            xmlhttp = new XMLHttpRequest();
        }
        else {
            console.log("Using ActiveXObject");
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.open("GET", "gethint.php?q=" + str, true);
        xmlhttp.send();

        xmlhttp.onreadystatechange = function() {
        console.log(xmlhttp.readystate);

        if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {
            console.log(xmlhttp.status);
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    }
}

If I change this line:

if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { ...

to this (via typo):

if (xmlhttp.readystate = 4 && xmlhttp.status == 200) { ...

Then it works, but it feels kind of like a "magic happens here" thing to be writing the code like this.

I'm following the PHP/AJAX tutorials at w3schools, and I've hit a bit of a roadblock at square one. Every time I call this function, the readystate is always undefined.

function showHint(str) {
    if (str.length == 0) {
        document.getElementById("txtHint").innerHTML = "";
            return;
        }

        var xmlhttp;

        if (window.XMLHttpRequest) {
            console.log("Using XMLHttpRequest");
            xmlhttp = new XMLHttpRequest();
        }
        else {
            console.log("Using ActiveXObject");
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.open("GET", "gethint.php?q=" + str, true);
        xmlhttp.send();

        xmlhttp.onreadystatechange = function() {
        console.log(xmlhttp.readystate);

        if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {
            console.log(xmlhttp.status);
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    }
}

If I change this line:

if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { ...

to this (via typo):

if (xmlhttp.readystate = 4 && xmlhttp.status == 200) { ...

Then it works, but it feels kind of like a "magic happens here" thing to be writing the code like this.

Share Improve this question asked Nov 29, 2012 at 23:22 Abion47Abion47 24.7k6 gold badges74 silver badges97 bronze badges 1
  • 1 xmlhttp.readystate = 4 (with single equals sign, assignment) assigns 4 to the .readystate property and is an expression that evaluates to 4, which is a truthy value and thus passes the if check. So it might as well have been if( true && xmlhttp.status == 200 ) or just if( xmlhttp.status == 200) – Esailija Commented Nov 29, 2012 at 23:39
Add a comment  | 

1 Answer 1

Reset to default 18

JS is case sensitive. You need to check the readyState property, not the readystate property.

    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        console.log(xmlhttp.status);
        document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
    }
发布评论

评论列表(0)

  1. 暂无评论