18

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.

6
  • That's the problem with subsets, they don't have it all. Commented Apr 6, 2009 at 9:25
  • all but you're 3rd example aren't valid JSON, as there is no key in the dictionary Commented Apr 6, 2009 at 12:18
  • Updated with a few more try's. Still getting parse-errors. Commented Apr 6, 2009 at 13:01
  • a json object is just an object literal? or not really? Commented Apr 6, 2009 at 13:27
  • I think I have the same problem. I just see the error message in FireBug: "[Exception... "'SyntaxError: JSON.parse' when calling method: [nsIDOMEventListener::handleEvent]" nsresult: "0x8057001c (NS_ERROR_XPC_JS_THREW_JS_OBJECT)" location: "<unknown>" data: no]". Any solution to this, yet? Commented May 13, 2009 at 9:44

8 Answers 8

10

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

Sign up to request clarification or add additional context in comments.

Comments

5

Firstly {[]}, {[null]}, and {null} won't work because they are all invalid JSON objects (as verified by the JSON validator).

The remaining objects are all valid JSON objects, so your success function should be getting invoked.

If you pass a non-array or array-like object object then the each function will enumerate your json object by its named properties. In the case of your three objects that each have a rows property this will be set to [], null, and {} respectively, none of which have a columnName attribute so an undefined error will be thrown.

Your {} object on the other hand has no properties, so shouldn't be causing an error because the each call will loop 0 times. What does the following line display if you add it as the first line in your success function?

alert(typeof json + ' ' + (json == null));

Comments

2

you are looking for this:

[]

an empty array. That is, if you plan to iterate over it right away, then what you need is an array.

Comments

2

Your web service may be returning null. I've found that returning null from a web service call returns a response with status code 200, "Ok", but jQuery throws a parseerror afterwards.

If this is the case, it has nothing to do with what you're sending up to the server, and everything with what the server is sending back.

If you're able to change the web service, you might want to try returning an empty JSON object, or a default value instead of Null. Alternatively, you could check for this error scenario in your error handler, knowing that this means your call returned null.

Comments

1

Did you verify if the JSON is returned correctly in the first place before the each? Set the empty value as {}, and verify if it is like that before .each

Also it would help to know how your JSON looks like when there is data.

Comments

1

Instead of:

$(json).each(function () { ... });

I think you want to be using:

$.each(json, function () { ... });

From the jQuery.each documentation:

This function is not the same as $().each() - which is used to iterate, exclusively, over a jQuery object. This function can be used to iterate over anything.

1 Comment

Type'o. But thanks--edited and saved. Not really an answer though.
0

I was experiencing similar problems when requesting valid JSON from the server.

My server was serving the content-type of text/javascript

I was not using the optional jQuery.ajax setting of 'dataType' so jQuery was interpretting the output a javascript (eg padded JSON), not neat JSON.

Adding a dataType:'JSON' to the settings object passed to jQuery's ajax method solved the problem.

Comments

0

Had this problem as well, and solved it by using the jsonserializer in my web service to format an empty string. result is was "\"\"" essentially "" ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.