简体   繁体   English

XMLHttpRequest.send()上的AJAX语法错误

[英]AJAX Syntax Error on XMLHttpRequest.send()

I'm working through an AJAX tutorial . 我正在研究AJAX教程 I have little web experience, so I was a bit surprised when something went wrong and I got no traceback, no log, nothing. 我几乎没有网络经验,所以当出现问题时我感到有些惊讶,并且没有回溯,没有日志,什么也没有。

I pulled out Firebug and it says I have a syntax error on both of my send() calls. 我拔出了Firebug,它说我的两个send()调用都出现语法错误。

The relevant (I think) parts of the code: 代码的相关部分(我认为):

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
    }
}

I've even went so far as to copy/paste these functions from the working tutorial, but I still get the same error. 我什至可以从工作教程中复制/粘贴这些功能,但是仍然遇到相同的错误。 What am I doing wrong? 我究竟做错了什么?

The exact error text is: 确切的错误文本为:

Syntax Error: 
    getChatText()       (line 38)
    handleSendChat()    (line 56)
receiveReq.send(null);  (line 38)

You're error is occurring inside of getChatText (which is at the top of the call stack in the error message). 您的错误发生在getChatText内部(该消息位于错误消息中的调用堆栈的顶部)。

You're probably calling eval with invalid syntax; 您可能使用无效的语法调用 eval you may have forgotten to wrap JSON in parentheses. 您可能已经忘记了将JSON包装在括号中。

You should use Firebug's debugger to track down the error. 您应该使用Firebug的调试器来跟踪错误。

EDIT : Try calling encodeURIComponent on lastMessage ; 编辑 :尝试在lastMessage上调用encodeURIComponent it may help. 它可能会有所帮助。 (and you should do it anyway to prevent URL injection) (您仍然应该这样做以防止URL注入)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM