Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.ajax() + empty JSON object = parse error

I get a parse error when using jQuery to load some JSON data. Here's a snippet of my code:

jQuery.ajax({
    dataType: "json",

    success: function (json)
    {
        jQuery.each(json, function ()
        {
            alert(this["columnName"]);
        });
    }
});

I get no errors when parsing a non-empty JSON object. So my guess is that the problem is with my serializer.


Question is: how do I format an empty JSON object which jQuery won't consider malformed?

This is what I've tried so far, with no success:

{[]}

{[null]}

{}

{null}


{"rows": []}

{"rows": null}

{"rows": {}}



UPDATE:

I can understand that I've been somewhat vague--let me try and clarify:

Parsing of the JSON object is not the issue here--JQuery is - I think.

jQuery throws a parse-error (invokes the error function). It seems like jQuery's internal JSON validation is not accepting any of the before mentioned objects. Not even the valid ones.

Output of the error function is:

XMLHttpRequest: XMLHttpRequest readyState=4 status=200
textStatus: parsererror
errorThrown: undefined

This goes for all of the before mentioned objects.

like image 957
cllpse Avatar asked Apr 06 '09 09:04

cllpse


1 Answers

The solution is to return a status code of 204 rather than 200 from the server, 204 is "no content" and it will return success while not trying to invoke the parser

like image 63
Adam Avatar answered Sep 18 '22 01:09

Adam