简体   繁体   中英

AJAX Syntax Error on XMLHttpRequest.send()

I'm working through an AJAX tutorial . 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.

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).

You're probably calling eval with invalid syntax; you may have forgotten to wrap JSON in parentheses.

You should use Firebug's debugger to track down the error.

EDIT : Try calling encodeURIComponent on lastMessage ; it may help. (and you should do it anyway to prevent URL injection)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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