0

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 changing 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.

Swader
  • 11,387
  • 14
  • 50
  • 84
  • 1
    So... you're using JSON, but you're trying to input that directly into html? Perhaps your data-type needs to be HTML? My guess is that your server is not outputting the proper Content-Type header for your data-type, and jQuery is trying to guess what the content-type is, using the headers. – Dominic Barnes Apr 12 '11 at 14:02
  • No, I just pass the data from PHP using JSON. The PHP echoes proper JSON headers and json_encodes. It worked great so far, on 1.4. – Swader Apr 12 '11 at 16:05
  • Yes, I know PHP will properly encode that JSON, but you need to also call `header('Content-Type: application/json');` so jQuery can properly guess your data-type. Either that, or you can use `$.getJSON`. – Dominic Barnes Apr 12 '11 at 16:10
  • Tried the latter, didn't work. Will try headers, up until now I've only been sending back a json_encode without the header. Thanks. Will try and post back. – Swader Apr 12 '11 at 16:11
  • Tried setting header, didn't work either. So far, only setting it to "text json" worked. – Swader Apr 13 '11 at 06:19

3 Answers3

1

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.

chiborg
  • 26,978
  • 14
  • 97
  • 115
0

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().

Dominic Barnes
  • 28,083
  • 8
  • 65
  • 90
0

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!

Swader
  • 11,387
  • 14
  • 50
  • 84