1

好吧,我正在尝试测试 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

我试图从互联网上找到解决方案,但没有任何帮助。因此,如果有人可以提供解决方案,我将不胜感激。

4

1 回答 1

1

这是使用 jQuery 的代码的一个端口,它将每秒轮询$_GET文本框中的值并将文本框中的值发送到服务器,并依赖于用名称填充一个 json 数组,然后如果返回的名称大于 1 个字符,则替换divMessage div 带有一条消息,希望对您有所帮助:

轮询.php

<?php
if(!empty($_GET['name']) && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'){

    header('Content-type: application/json');

    $userNames = array('YODA', 'AUDRA', 'BOGDAN', 'CRISTIAN');

    if (in_array(strtoupper($_GET['name']), $userNames)){
        $data = array('name'=>$_GET['name']);
        echo json_encode($data);
    }else{
        echo json_encode(array('name'=>''));
    }
    die;
}
?>
<!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 Polling with jQuery & PHP (json)</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" charset="utf-8"></script>
<script>
//jQuery stuff
function poll(){
    setTimeout(function(){
        var name = $("#name").val();
        if(name.length == 0){name = 'null';}

        $.ajax({ url: "polling.php?name=" + name, cache: false,
        success: function(data){
            if(data.name.length >= 1){
                $("#divMessage").replaceWith('<div id="divMessage">Welcome '+ data.name +'!<div>');
            }else{
                $("#divMessage").replaceWith('<div id="divMessage">I Dont know you.<div>');
            }
            poll();
        }, dataType: "json"});
    }, 1000);
}
$(document).ready(function(){
    poll();
});
</script>
</head>
<body>
Server wants to know your name: <input type="text" id="name" />

<div id="divMessage"><div>
</body>
</html>
于 2012-05-20T06:17:49.700 回答