Jquery 1.5 breaks old 1.4 $.get calls
I recently upgraded to 1.5, and functions like:
function showData(id) {
$.get("/url/getdata", {id : id}, function(data) {
$("#dialogData").html(data);
$("#dialogData").dialog({width: 500, modal: true, zIndex:22000});
}, "json");
}
no longer work with the addition of 1.5.
Looking at firebug, the correct data is returned, but the function breaks after entering the callback. Everything simply stops.
How can I fix this? I read about the changes to Ajax call in 1.5, but I have over a thousand such calls through my project -> I cannot even begin to think about hunting them all down and changing them, let alone bug testing it all.
Edit: Oddly enough, if I put "text json" instead of "json" for dataType, it works. I don't think going through all my code and changi开发者_StackOverflow社区ng that is a viable option...
Edit 2: Instead of returning json_encode($string), I tried returning json_encode(array("string" => $string)), and then I did $(element).html(data.string). This did not work either, and the JSON data I got with this call was valid on jsonlint.com
Edit 3: Tried setting headers prior to the json_encode output, didn't work. So far the only solution has been setting the datatype to text json.
You use the data in with the html()
function, but expect JSON data. Have a look at the mime type that comes back and if you really expect the data to be parsable JSON.
Sounds like you need to set the Content-Type
header on the server-side. PHP can easily do this, just call header('Content-Type: application/json');
sometime before you start outputting your encoded JSON data.
jQuery will see that header, and automatically parse your JSON for you. I'm guessing that in the never version, it probably is more strict with dataType
. If it tried to parse it as HTML (since PHP sends Content-Type: text/html
by default) then it will probably hit an error.
Once you've got your JSON data, you can build your HTML to use with html()
.
I got the culprit. It was queue.js, an ajax extension that allowed queueing and abortion of ajax calls. It extended jquery's ajax, so ajax ended up being broken in this minute way. Now to find a way to restore compatibility and I'm all good.
Thanks for the effort all!
精彩评论