나는 w3schools에서 PHP/AJAX 튜토리얼을 따르고 있으며, 사각형 하나에 약간의 장애물이 있습니다. 이 함수를 호출 할 때마다 readystate는 항상 정의되지 않습니다.XMLHttpRequest ReadyState는 항상 정의되지 않음
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 (xmlhttp.readystate == 4 && xmlhttp.status == 200) { ...
(를 통해 오타) : 다음
if (xmlhttp.readystate = 4 && xmlhttp.status == 200) { ...
을 그것은 작동하지만 "마법은 여기에 어떻게"와 같은이 종류의 느낌 이런 식으로 코드를 작성해야합니다.
'xmlhttp.readystate = 4' .readystate'은''4' 속성 할당하고 truthy' '값이'4' 계산되는 식이다 (단일 등호는 할당 기호) 따라서 if 점검을 통과합니다. 따라서 if (true && xmlhttp.status == 200) 또는 단지 if (xmlhttp.status == 200) 일 수도 있습니다. – Esailija