gpt4 book ai didi

php - 无法读取未定义的属性 'readyState'

转载 作者:行者123 更新时间:2023-11-28 15:57:05 26 4
gpt4 key购买 nike

花了几个小时找出问题所在,但失败了..因为我是 Ajax 新手,所以我不知道我看到了什么。真的需要你们找到这个错误。

HTML

    <!doctype html>
<head>
<script type="text/javascript" src="foodstore.js"></script>
</head>

<body onload="process()">
<h3>The Chuff Bucket</h3>
Enter the food you would like to order:
<input type="text" id="userInput">
<div id="underInput" />
</body>

</html>

JavaScript:

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject(){
var xmlHttp;

if(window.ActiveXobject){
try{
xmlHttp = new ActiveXobject("Microsoft.XMLHTTP");

}catch(e){
xmlHttp = false;
}
}else{
try{
xmlHttp = new XmlHttpRequest();
}catch(e){
xmlHttp = false;
}
}

if(!xmlHttp){
alert("can't create that object");
}
else{
return xmlHttp;
}
}

function process(){
if(xmlHttp.readyState==0 || xmlHttp.readyState==4){
food = encodeURIComponent(document.getElementById("userInput").value);
xmlHttp.open("GET", "foodstore.php?food=" + food, true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);
}else{
setTimeout('process()', 1000);
}

}

function handleServerResponse(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
message = xmlDocumentElement.firstChild.data;
document.getElementById("underInput").innerHTML = "<span style='color:blue'>" + message + "</span>";
setTimeout('process()', 1000);

}else{
alert('something went wrong');
}
}
}

PHP(我认为这个文件导致了问题)

<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';

echo '<response>';
$food = $_GET['food'];
$foodArray = array('tuna','bacon','beef','ham');

if(in_array($food,$foodArray)){
echo 'We do have ' .$food. '!';
}else if($food == ''){
echo 'Enter a food name.';
}else
{
echo "no, we don't sell " .$food. "!";
}

echo '</response>';

?>

最佳答案

JavaScript 区分大小写。由于您尝试创建的某些对象的大小写不正确,您遇到了一些语法错误:

ActiveXobject

应该是

ActiveXObject
^

XmlHttpRequest

应该是

XMLHttpRequest
^^

最终结果是您尝试创建不存在的东西,导致 xmlHttp 变量始终为 falseundefined.

关于php - 无法读取未定义的属性 'readyState',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18338041/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com