Background:
In this Javascript code, the success
event is never fired:
$.ajaxSetup({cache:false,
success:function(d) {
console.log("ok from setup with data "+d.toSource())
},
complete:function(xhr,ts){
console.log("Ajax finished reponse:"+xhr.responseText)
},
error:function(jqXHR, textStatus, errorThrown){
console.log("Error")
}
});
$.getJSON("test2.php",{},function(data){
//some code here
});
When I do it this way, it works:
$.ajaxSetup({cache:false,
complete:function(xhr,ts){
console.log("Ajax completado con:"+xhr.responseText)
},
error:function(jqXHR, textStatus, errorThrown){
console.log("Error")
}
});
$.getJSON("test2.php",{},
function(data){
//some code here
}).success(function(d){
console.log("success applied directly, data "+d.toSource())
}
);
In both cases the complete event is always fired and the error one never.
However, in the second code the success is fired.
Obviously for .get()
method it's the same.
<?php header("Content-Type:application/json;charset=utf-8");
//or whatever text/x-json text/plain, even text/html, jquery do the dirty job
echo json_encode(array("stat"=>"1")) ?>
My objectives:
The problem is strange, any ideas?
I read all these questions:
And I'm pretty sure none of them are my problem.
Take a look at the ajaxSetup documentation: http://api.jquery.com/jQuery.ajaxSetup/
Note: Global callback functions should be set with their respective global Ajax event handler methods—.ajaxStart(), .ajaxStop(), .ajaxComplete(), .ajaxError(), .ajaxSuccess(), .ajaxSend()—rather than within the options object for $.ajaxSetup().
I think that's your problem right there.
UPDATE
If you want your ajax handlers to be global for any ajax request on the page, do something like this:
$(document).ajaxSuccess(function(d){
console.log("ok from setup with data "+d.toSource());
});
$(document).ajaxSuccess(...)
Dan A. 2012-04-03 22:49