Background:
- Using jQuery 1.7 client side
- PHP server side
- Using json responses with json_encode php function
- The content-type header is correct, any of these works: text/plain,text/x-json,application/json.
- There's no errors thrown from my php code
- Am working on Firefox 11
- Am using the js console and the other web's developer tools
- HTTP/1.1 200 OK
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 code:
<?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:
- I want to fire the same success event to all ajax requests
- I want to use the json data returned by the request in my success event, and if it is possible, without convert the raw jqXHR responseText to a json again
The problem is strange, any ideas?
I read all these questions:
- Ajax success event not working
- AjaxSetup never execute the success function
- Does jQuery ajaxSetup method not work with $.get or $.post?
- $.ajax function's success: not firing
And I'm pretty sure none of them are my problem.