SyntaxError: expected expression, got ')'

Related searches

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.

JavaScript Error Help: “SyntaxError: expected expression, got , [This thread is closed.] I have a JS error on my website: “SyntaxError: expected expression, got '<'.” I'm not sure… SyntaxError: expected expression, got ')' usually cause by something like . exeFunction(a,b,) See if your form submit function ajax causing such error

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

});

SyntaxError: expected expression, got '.', No matter what I try, I cant get rid of this SyntaxError: SyntaxError: expected expression, got '.' here is my code $(document).ready(function()� SyntaxError: expected expression, got '<' <!DOCTYPE html> Ask Question Asked 4 years, 2 months ago. Viewed 999 times 0. 1. I have gone through many examples and tried

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>

How I resolve SyntaxError: expected expression, got '<' � Issue #81 , Description Have been dealing with the evil SyntaxError: expected expression, got '<' on Firefox in the last couple of days and had totally no� The JS interpreter tries to “execute” the tag, which looks like an expression beginning with a less-than sign, hence the error: SyntaxError: expected expression, got '<'

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

That seems to have something do do with a popup form. If it’s legit file (which I doubt), you can disable all plugins then turn them back on, one at a time, to see which might be associated with it.

Hello, I'm building with PHP an HTML string and then I want to put it in the html of the div (On this moment I don't know anymore how this called, sorry):

I got SyntaxError: expected expression, got '<' error in the console when i'm executing following node address var port = server.address().port })

SyntaxError: expected expression, got keyword 'else'. Below is the code. var ageJohn = 25; var heightJohn = 165; var ageSmith = 60; var heightSmith = 180; var ageMalli = 24; var heightMalli = 170; var scoreJohn = heightJohn + 5 * ageJohn; var scoreSmith = heightSmith + 5 * ageSmith; var scoreMalli = heightMalli + 5 * ageMalli; if (scoreJohn > scoreSmith && scoreJohn > scoreMalli); { console.log('John wins with ' + scoreJohn); } else if (scoreSmith > scoreJohn && scoreSmith > scoreMalli); {

Comments
  • I am not fluent in javascript, but you have a semicolon after your first function }; close bracket. Not sure if that would cause the error.
  • console log xml to see what your returned data is - console.log(xml);
  • @Rasclatt the semicolon wouldn't cause the error, but you're right, in this case it is not appropriate to put a };. If you are defining an anonymous function as a variable, then }; is appropriate. (i.e, var f = function(){};)
  • Yeah I tried it on my computer and it didn't throw any error.
  • Oh, I see. You are calling eval('(' + xml + ')'). xml must be an empty string. Where is it coming from and what are you trying to do with it? eval is typically meant to run a piece of code and is generally not a good idea. What kind of data are you expecting from server.php?
  • This is the correct answer. Using eval is terribly wrong in this case and this answer fixes it.
  • I tried removing the comma but it doesn't seems to help.
  • No, jQuery transforms the data object into application/x-www-form-urlencoded format for use in the POST body. OP hasn't indicated this is a problem at all
  • k.. good to know. I have learned to stringify every model that gets passed into jQuery ajax calls or it doesn't work for me.. quite possible that's related to the web api i talk to.. why i said it was a guess.