xmlHttp.status==200从来都不是真的

xmlHttp.status==200 is never true

本文关键字:真的 status xmlHttp      更新时间:2024-03-14

我有一个项目,我正在使用ajax和php。问题是XMLHttpRequest()的状态永远不会是200。我搜索的解决方案都不起作用,所以我在这里询问。我在这里复制/粘贴ajax发送变量的脚本和php文件。脚本

function categoryfilter(int) {
    if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    } else {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlHttp.readyState == 4 $$ xmlHttp.status == 200) {
            document.getElementById("test").innerHTML = xmlHttp.responseText;
            alert("GOOD");
        }
    }
    xmlHttp.open("GET","ajax_livecategorysearch.php?category=" + int, true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send();
}

php文件

<?php echo "whatever"; ?>

html代码

<input id="category" name="category" type="radio" checked="checked" onclick="categoryfilter(this.value);" >

当我删除条件xmlHttp.status==200时,函数返回错误404找不到页面。

您在代码中有一个拼写错误:

if (xmlHttp.readyState==4 $$ xmlHttp.status==200)

应该是

if (xmlHttp.readyState==4 && xmlHttp.status==200)