missing ] after element list parsing JSON

I was working on a little web application front end submitting a form and processing a result asynchronously using jQuery. I wrote a little JavaScript snippet that looks something like the following:

$.post(
    "submitForm.jsp",
    theForm.serialize(),
    function(data){
        var res = eval('(' + data + ')');
        // do something with the result
    }
);

Yeah, I know, eval() probably isn’t the best choice here but we need to support IE7 which doesn’t support JSON.parse natively. Anyway, after submitting I get the following error:

error: missing ] after element list parsing JSON

Turns out that jQuery will try to figure out if the result is JSON and parse it automatically. In this case data is already a JavaScript object and not a JSON string waiting to be parsed.

Category(s): JavaScript, Programming
Tags: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *