Bug Tracker

Opened 9 years ago

Closed 9 years ago

Last modified 8 years ago

#8014 closed bug (worksforme)

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

Reported by: maher@… Owned by: maher@…
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 + ")" );
}

Change History (4)

comment:1 Changed 9 years ago by Rick Waldron

Owner: set to maher@…
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!

comment:2 Changed 9 years ago by danheberden

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

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

Last edited 9 years ago by danheberden (previous) (diff)

comment:3 Changed 9 years ago by Rick Waldron

Resolution: worksforme
Status: pendingclosed

comment:4 Changed 9 years ago by jitter

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.

Note: See TracTickets for help on using tickets.