Reputation: 2103
Given an ajax call such as:
$.ajax(
{
url:"MyWebService.blah",
data: {"data":"awesome"},
success : function(responseText)
{
var myJsonObj = $.parseJSON(responseText);
//do stuff with myJsonObj
}
});
This was working fine. I updated jQuery to 1.9 today (I was on 1.6 for a while) as a possible fix to Safari all of the sudden not supporting various toggle functionality (something about eventLayer.X no longer supported), and now all my ajax calls are throwing the following javascript error:
Uncaught Syntax Error: Unexpected token o
After a little research and some testing, I discovered that "responseText" in my code above is now a JSON object, not a string. So the error makes sense, but I'm trying to wrap my head around this. Did jQuery really change the default return type? I checked the documentation:
http://api.jquery.com/jQuery.ajax/
and dataType is defaulted to "Intelligent Guess". I can see how that might be convenient, but I also don't like it.
So here are my questions:
This is a pretty fundamental change that affects a lot of code. I will go through my code and remove any instance of parsing my returned data to JSON, but this whole thing is a little unnerving. Was I mistaken in not specifying a dataType? I suppose it is a good practice to enforce a dataType instead of relying on default, but... wow. Am I alone on this, or was that a tad presumptuous of a change on the part of jQuery?
Upvotes: 5
Views: 2400
Reputation: 95059
jQuery automatically detects what the dataType is based on what was returned if no dataType was set. Most likely 1.9 just improved that detection to properly detect what you are returning as json. It's best to always give a datatype to ensure you'll always get consistent results.
Upvotes: 6