jQuery AJAX & Internet Explorer
Wednesday, May 26th, 2010Internet Explorer getJSON trouble
This piece of code initially did not work for me on IE:
$.getJSON("../inc/exercise_ajax.php?" + $(this).serialize(), function(json)
FireFox worked without problems.
The reason is that my JSON code was not well-formed. It had a comma (',') too much.
Here's the code which I use in PHP to create my JSON (braces omitted):
foreach($answers as $question => $answer){
$outstr .= '"q'.$question.'": ';
if (isset($_GET[$question])){
if ($_GET[$question] == $answer) $outstr .= 'true';
else $outstr .= 'false';
} else $outstr .= 'null';
$outstr .= ',';
}
echo chop($outstr,',');
You have to chop off the trailing comma – if you don't, IE will choke on it and your callback function won't be executed.
Internet Explorer & AJAX caching
Yes, Internet Explorer caches your requests. You've got to empty the browser cache before testing the fix I mentioned. Also, you should append a unique parameter to suppress caching, i.e. the current timestamp. Example:
var now = new Date();
// submit with AJAX
$.getJSON("../inc/exercise_ajax.php?ts=" + now.getTime() + '&' + $(this).serialize(), function(json)