简体   繁体   中英

SyntaxError: expected expression, got ')'

I've been stuck at this error for a few days and still couldn't figure out what is wrong. Would be great if someone could just point me to the right direction of solving this issue.

Update: I realise that error is gone when I commented "addMessages(xml)" in the updateMsg() function. How do I make it work then?

Error: http://i.imgur.com/91HGTpl.png

Code:

$(document).ready(function () {
    var msg = $("#msg");
    var log = $("#log");
    var timestamp = 0;

    $("#name").focus();

    $("#login").click(function() {
        var name = $("#name").val();
        if (!name) {
            alert("Please enter a name!");
            return false;
        }

        var username = new RegExp('^[0-9a-zA-Z]+$');

        if (!username.test(name)){
            alert("Invalid user name! \n Please do not use the following characters \n `~!@#$^&*()=|{}':;',\\[\\].<>/?~@#");
            return false;
        }

        $.ajax({
            url: 'login.php',
            type: 'POST',
            dataType: 'json',
            data: {name: name},
            success: function() {
                $(".login").hide();
            }
        })
        return false;
    });

    $("#form").submit(function() {
        if (!msg.val()) {
            return false;
        }

        $.ajax({
            url: 'add_message.php',
            type: 'POST',
            dataType: 'json',
            data: {message: msg.val()},
        })

        msg.val("");

        return false

    });

    window.setInterval(function () {
        updateMsg();
    }, 300);

    function updateMsg() {
        $.post('server.php', {datasize: '1024'}, function(xml) {
            addMessages(xml);
        });
    }

    function addMessages(xml) {

        var json = eval('('+xml+')');

        $.each(json, function(i, v) {

            tt = parseInt(v.time);

               if (tt > timestamp) {
                console.log(v.message);
                appendLog($("<div/>").text('[' + v.username + ']' + v.message));
                timestamp = tt
            }
        });
    }

    function appendLog(msg) {
        var d = log[0]
        var doScroll = d.scrollTop == d.scrollHeight - d.clientHeight;
        msg.appendTo(log)
        if (doScroll) {
            d.scrollTop = d.scrollHeight - d.clientHeight;
        }
    }
});

It might help to read up on eval a bit. It looks like it doesn't do what you think it does.

eval() is a dangerous function, which executes the code it's passed with the privileges of the caller.

Also

There are safer (and faster!) alternatives to eval() for common use-cases.

It looks like what you're trying to do is get data from the server in the form of JSON. You'll need to make sure that your server returns something that is valid JSON, which you can verify here . Most server-side programming languages have a library that will turn an object into JSON to make that a piece of cake. Here's an example for php .

On the client-side, you'll need to change var json = eval('(' + xml + ')'); to var json = JSON.parse(xml); This will give you the javascript version of your php/perl/python/etc object. If it's an array, you can then iterate through it with a for loop, Array.prototype.forEach , or a variety of functions from different libraries, such as $.each or _.each .

SyntaxError: expected expression, got ')' usually cause by something like

exeFunction(a,b,) 

See if your form submit function ajax causing such error

$("#form").submit(function() {
    if (!msg.val()) {
        return false;
    }

    $.ajax({
        url: 'add_message.php',
        type: 'POST',
        dataType: 'json',
        data: {message: msg.val()},     <-------
    })

    msg.val("");

    return false

});

If you are triggering the java script on click or trigger any click. sometimes missing of 0 gives the above error.

<a href="javascript:void(0);">delete</a>

would JSON.stringify({datasize: '1024'}) do the trick? just a guess

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