Skip to main content

Bug Tracker

Side navigation

#8014 closed bug (worksforme)

Opened January 19, 2011 10:19PM UTC

Closed January 19, 2011 10:39PM UTC

Last modified March 14, 2012 07:57PM UTC

jQuery.parseJSON() doesn't work on all servers

Reported by: maher@coolworlds.net Owned by: maher@coolworlds.net
Priority: low Milestone: 1.next
Component: ajax Version: 1.4.4
Keywords: Cc:
Blocked by: Blocking:
Description

I have an Arabic contact script that uses Ajax to retrieve a response from the server after filling the form.

On some apache servers, jQuery.parseJSON() throws an invalid json excepion for the same json it parses perfectly on other servers. This exception is thrown only on chrome and IE.

The json content gets encoded using php's json_encode() function. I tried sending the correct header with the json data and setting the unicode to utf-8, but that didn't help.

This is one of the json responses I try to parse (removed the second part of if because it's long):

{"pageTitle":"\\u062e\\u0637\\u0623 \\u0639\\u0646\\u062f \\u0627\\u0644\\u0625\\u0631\\u0633\\u0627\\u0644 !"
}

Note: This language of this data is Arabic, that's why it looks like this after being parsed with php's json_encode().

You can try to make a request in the examples given down and look at the full response data using firebug or webkit developer tools. The response passes jsonlint!

Finally, I have two urls using the same version of the script, try to browse them using chrome or IE to see the error in the broken example.

The working example : http://namodg.com/n/

The broken example: http://www.mt-is.co.cc/my/call-me/

I would like to note that I manged to fix this by using the old eval() to parse the content, I released another version with this fix, it was like this:

// Parse the JSON data
try
{
    // Use jquery's default parser
    data = $.parseJSON(data);
}
catch(e)
{
    /*
     * Fix a bug where strange unicode chars in the json data makes the jQuery
     * parseJSON() throw an error (only on some servers), by using the old eval() - slower though!
     */
    data = eval( "(" + data + ")" );
}
Attachments (0)
Change History (4)

Changed January 19, 2011 10:21PM UTC by rwaldron comment:1

owner: → maher@coolworlds.net
status: newpending

Thanks for taking the time to contribute to the jQuery project! Please provide a reduced jsFiddle test case to help us assess your ticket!

Additionally, test against the jQuery 0 GIT version to ensure the issue still exists. Be Excellent to eachother!

Changed January 19, 2011 10:36PM UTC by danheberden comment:2

_comment0: Perhaps there is an issue with the character encoding on your site? \ \ Test-case with your data: http://jsfiddle.net/danheberden/PmhM8/1295476669398475

Perhaps there is an issue with the character encoding on your site?

Test-case with your data: http://jsfiddle.net/danheberden/LP8UJ/

Changed January 19, 2011 10:39PM UTC by rwaldron comment:3

resolution: → worksforme
status: pendingclosed

Changed January 19, 2011 11:10PM UTC by jitter comment:4

component: unfiledajax
priority: undecidedlow

Haha. Forgot to press submit. Posting anyway.

I tried to reproduce your problem but this test case (using your json snippet) works just fine. Be it with 1.4.4 or 1.5b1.

As you didn't provide detailed information on what needs to be done on the two sites you linked to, I just did a quick test on http://www.mt-is.co.cc/my/call-me/ and filled in some stuff on form and hit submit. What I noticed is that the returned data had a content type of text/html instead of application/json and also wasn't valid json at all.

So I assume this is some problem with the response by your server (wrong content type, invalid json). Also try explicitly setting dataType: "json" on your ajax-request to let jQuery know that it should handle the response as json despite the wrong content type returned by the server.

If you still think this is a bug in jQuery core, please submit a reduced test case, which reproduces the issue you are experiencing, on http://jsfiddle.net. So that we can investigate this issue further.