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.

Tags: , , , , ,

Friday, April 13th, 2012 JavaScript, Programming

Leave a Reply

 

Comments are moderated due to spammers. Your comments will not appear until I review and approve them. If you left a question I will answer as best I can so be sure to check back.