【发布时间】:2011-04-05 00:51:48
【问题描述】:
我正在处理an AJAX tutorial。我几乎没有网络经验,所以当出现问题时我有点惊讶,我没有回溯,没有日志,什么都没有。
我拿出 Firebug,它说我的两个 send() 调用都有语法错误。
代码的相关(我认为)部分:
function getChatText() {
if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
receiveReq.open("GET", 'getChat.php?chat=1&last=' + lastMessage, true);
receiveReq.onreadystatechange = handleReceiveChat;
receiveReq.send(null); // <--- Firebug says an error is here
}
}
function sendChatText() {
if (sendReq.readyState == 4 || sendReq.readyState == 0) {
sendReq.open("POST", 'getChat.php?chat=1&last=' + lastMessage, true);
sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
sendReq.onreadystatechange = handleSendChat;
var param = 'message=' + document.getElementById('txt_message').value;
param += '&name=John Doe';
param += '&chat=1';
sendReq.send(param); // <--- and also here
}
}
我什至从工作教程中复制/粘贴这些函数,但我仍然遇到同样的错误。我做错了什么?
确切的错误文本是:
Syntax Error:
getChatText() (line 38)
handleSendChat() (line 56)
receiveReq.send(null); (line 38)
【问题讨论】:
-
这不是 语法 错误。错误的确切文本是什么?
-
我强烈建议将 jQuery 用于与 AJAX 相关的任务。它简化了十倍的代码,并且更兼容跨浏览器。 api.jquery.com/jQuery.ajax
-
@SLaks:我不认为是这样,但这就是错误所说的。我已添加错误消息。
-
@Dutchie432:jQuery 的下载链接似乎已损坏。
标签: javascript ajax syntax