15

I have just updated to JQuery 1.5 and all my ajax calls that return JSON and a number of plugins instantly broke.

In my pre-1.5 code, I specified the dataType like:

dataType: "json"

Changing the dataType to:

dataType: "text json"

Fixes the problem but I do not want to manually change the plugins as this will affect upgrades.

Is there a way of handling this better with less disruption?

user229044
  • 232,980
  • 40
  • 330
  • 338
dagda1
  • 26,856
  • 59
  • 237
  • 450
  • 2
    If they return JSON, it would be AJAJ calls, right? ;) – Guffa Feb 10 '11 at 10:01
  • If you do not specify the dataType in plugin rather use Global AJax settings object to do this, wouldn't this solve the problem. – Furqan Hameedi Feb 10 '11 at 10:02
  • If you own the source files that are generating the data for the AJAX calls, go add the `Content-Type: application/json` header and it should fix all your scripts (this should be done whenever outputting JSON anyways). – Brad Christie Feb 10 '11 at 10:05
  • it's very unlikely that this is caused by jQuery 1.5. `dataType` "json" is predefined and will still parse incoming **valid** JSON data into javascript objects. – jAndy Feb 10 '11 at 10:07
  • @jAndy: Yes, it's most likely not caused by jQuery 1.5 on it's own, but there may be an already existing discepancy between the requested data type and the returned content type, which jQuery 1.5 might handle differently. – Guffa Feb 10 '11 at 11:36
  • I have checked the content-type in fiddler and it is indeed application/json; charset=utf-8 – dagda1 Feb 14 '11 at 09:38

3 Answers3

7

This problem is caused by the jQuery validation plugin and how it extends the ajaxSettings object for its own use (changes json calls to jsonp calls). There will hopefully be an official update to the validation plugin soon, but in the meantime, there's a patch available here that fixes the problem:

https://github.com/bigfix/jquery-validation/commit/9aa53f9241224ba349f9fe1b2c801be0f431b63b

Edit:

jQuery Validation Plugin, compatible with jQuery 1.5.1, available on git hub: https://github.com/jzaefferer/jquery-validation

Edit #2: jQuery Validation Plugin 1.8, compatible with jQuery 1.5.1 (and earlier) is released:

Read about it: http://bassistance.de/2011/03/25/release-validation-plugin-1-8/

Download it: http://bassistance.de/jquery-plugins/jquery-plugin-validation/

James H
  • 2,401
  • 3
  • 22
  • 20
  • Thanks for this answer! Yep -- even when returning the correct Content-Type (application/json) from the server, the current jQuery validation plug-in (1.7) causes it to break. – Nicholas Piasecki Mar 02 '11 at 14:30
4

I came across this JQuery bug which fixes the problem http://bugs.jquery.com/ticket/8084.

Adding the following code after the JQuery script declaration fixes the problem:

$.ajaxSetup({ jsonp: null, jsonpCallback: null });

dagda1
  • 26,856
  • 59
  • 237
  • 450
3

Untested code, add this after jQuery is included, and before your custom code runs.

jQuery.ajaxSetup({
  converters: {
    "json": jQuery.parseJSON,
    "* json": jQuery.parseJSON
  }
});
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • That's along the same line as something that I was about to suggest... The new jQuery version might behave differently if the dataType is different from the content type of the data that arrives, i.e. try to make a conversion before parsing as the specified type. – Guffa Feb 10 '11 at 10:23
  • This does not work either unfortunately. I have checked the content-type in fiddler and it is indeed application/json; charset=utf-8. – dagda1 Feb 14 '11 at 09:32