我正在关注“构建响应式 Web 应用程序 AJAX 和 PHP”一书中的 PHP/AJAX 聊天教程,但遇到以下错误:
TypeError: Cannot read property 'documentElement' of null
我相当确定它在 javascript 文件中引用了这段代码:
function readMessages(){
var response = XmlHttpGetMessage.responseText;
if(response.indexOf("ERRNO") >= 0 || response.indexOf("error:")>= 0 || response.length==0){
throw(response.length == 0 ? "Void server response." : response);
}
response = XmlHttpGetMessage.responseXML.documentElement;
clearChat = response.getElementByTagName("clear").item(0).firstChild.data;
if(clearChat == "true"){
$("#scroll").html("");
lastMessageID = -1;
}
idArray = response.getElementByTagName("id");
//nameArray = response.getElementByTagName("name");
timeArray = response.getElementByTagName("time");
messageArray = response.getElementByTagName("message");
displayMessages(idArray, nameArray, timeArray, messageArray);
if(idArray.length>0){
lastMessageID = idArray.item(idArray.length - 1).firstChile.data;
setTimeout("requestNewMessages();", updateInterval);
}
}
这是我发送的 ajax 请求:
if(cache.length>0){
params = cache.shift();
}else{
params="mode=RetrieveNew" +
"$id=" +lastMessageID;
}
XmlHttpGetMessage.open("POST", chatURL, true);
XmlHttpGetMessage.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
XmlHttpGetMessage.onreadystatechange = handleRecievingMessages;
XmlHttpGetMessage.send(params);
这是 PHP 文件:
<?php
require_once('init.php');
$mode = $_POST['mode'];
id=0;
$chat = new Chat();
if($mode=='SendAndRetrieveNew'){
$user_id = $_POST['user_id'];
$message = $_POST['message'];
$color = $_POST['color'];
$id = $_POST['id'];
if($user_id != '' && $message != '' && $color != ''){
$chat->postNewMessage($user_id, $message, $color);
}
}elseif($mode=='DeleteAndRetrieveNew'){
$chat->deleteAllMessages();
}elseif($mode=='RetrieveNew'){
$id = $_POST['id'];
}
if(ob_get_length()){ob_clean();}
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . 'GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Content-Type: text/xml');
echo $chat->getNewMessages($id);
?>
这里是句柄接收消息函数函数handleRecievingMessages(){
if(XmlHttpGetMessage.readyState == 4){
if(XmlHttpGetMessage.status == 200){
try{
readMessages();
}catch(e){
displayError(e.toString());
}
}else{
displayError(XmlHttpGetMessage.statusText);
}
}
}
最后是我的 getNewMessages 函数
public function getNewMessages($id=0){
$queryArray = array($id);
if($id>0){
//only get the new messages
$query =
' SELECT message_id, user_id, message, DATE_FORMAT(posted_on, "%H:%i:%s")
AS posted_on FROM chat WHERE message_id > '
. $id .
' ORDER BY message_id ASC';
}else{
//on the first load only retrieve the last 50 messages from server
$query =
' SELECT message_id, user_id, message, posted_on
FROM (SELECT message_id, user_id, message,s DATE_FORMAT(posted_on, "%H:%i:%s")
AS posted_on FROM chat ORDER BY message_id DESC LIMIT 50) AS Last50
ORDER BY message_id ASC';
}
$query = $this->_database->_link->prepare($queryString);
$query->execute();
$response = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
$response .= '<response>';
$response .= $this->isTableCleared($id); // output the clear flag
// if we have any results, loop through all the fetched messages to build the result message
if($query->rowCount()){ //get number of rows in a result, aka check for results
while ($row = $query->fetch(PDO::FETCH_ASSOC)){ //get results as an associative array
$id = $row['message_id'];
$userName = $row['user_id'];
$time = $row['posted_on'];
$message = $row['message'];
$response .= '<id>' . $id . '</id>' .
'<color>' . $color . '</color>' .
'<time>' . $time . '</time>' .
'<name>' . $userName . '</name>' .
'<message>' . $message . '</message>';
}
$query->close(); // close the database connection
}
$response .= '</response>';
return $response;
}
当我使用 firebug 进行调试时,我得到以下文本响应:
"<br /> <b>Parse error</b>: syntax error, unexpected ';', expecting T_FUNCTION in `<b>C:\xampp\htdocs\thinkaztech\includes\chat.class.php</b> on line <b>93</b><br /> "`
但是我没有93行?
我不确定错误是什么,我已经很好地连接到数据库并且我的 chat.class.php 文件似乎被正确调用了。感谢您提前提供任何有用的建议!