我正在阅读“构建响应式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);
?>
这是句柄接收消息功能 function 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文件似乎被正确调用了。感谢您提前提供任何有用的建议!
答案 0 :(得分:1)
看起来像是一个错字:
params="mode=RetrieveNew" +
"$id=" +lastMessageID;
^ should be a & not a $