好吧,我正在尝试测试 AJAX 代码并运行本书中的示例示例:
AJAX 和 PHP 作者:Audra Hendrix;波格丹布林扎里亚;克里斯蒂安·达里
现在有三个文件:
1) 索引.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>AJAX with PHP, 2nd Edition: Quickstart</title>
<script type="text/javascript" src="quickstart.js"></script>
</head>
<body onload='process()'>
Server wants to know your name:
<input type="text" id="myName" />
<div id="divMessage" />
</body>
</html>
2) 快速启动.js
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("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}
function process(){
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
name = encodeURIComponent(
document.getElementById("myName").value);
xmlHttp.open("GET", "quickstart.php?name=" + name, 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;
helloMessage = xmlDocumentElement.firstChild.data;
document.getElementById("divMessage").innerHTML =
'<i>' + helloMessage
+ '</i>';
setTimeout('process()', 1000);
}
else {
alert("There was a problem accessing the server: " +
xmlHttp.statusText);
}
}
}
3) 快速启动.php
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
echo '<response>';
$name = $_GET['name'];
$userNames = array('YODA', 'AUDRA', 'BOGDAN', 'CRISTIAN');
if (in_array(strtoupper($name), $userNames))
echo 'Hello, master ' . htmlentities($name) . '!';
else if (trim($name) == '')
echo 'Stranger, please tell me your name!';
else
echo htmlentities($name) . ', I don\'t know you!';
echo '</response>';
?>
但是当我将它上传到我的站点时,相同的代码向我显示了问题:000Webhost.com
我尝试了 3 种不同的浏览器:
1)谷歌浏览器显示没有错误,但代码也没有运行。
2) FireFox 显示此错误为: XML Parsing Error: junk after document element Location: http://flavorsofsoul.site88.net/ajax/quickstart/quickstart.php?name=第 3 行,第 1 列:
3) IE8 显示:网页错误详情
消息:需要对象行:65 字符:7 代码:0 URI: http: //flavorsofsoul.site88.net/ajax/quickstart/quickstart.js
我试图从互联网上找到解决方案,但没有任何帮助。因此,如果有人可以提供解决方案,我将不胜感激。