AJAX程序不在网站上运行,但在本地WAMP服务器上运行良好

时间:2012-05-20 05:23:18

标签: php javascript ajax wamp wampserver

我正在尝试测试AJAX代码并运行本书中的示例示例:

AJAX和PHP作者:Audra Hendrix; Bogdan Brinzarea;克里斯蒂安达里

现在有三个文件:

1)Index.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)quickstart.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)quickstart.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解析错误:文档元素后的垃圾位置: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

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

1 个答案:

答案 0 :(得分:1)

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

<强> polling.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>