I'm doing quite a few jquery ajax calls and I wanted to catch the 401 status code on all of them. So, I'm using $.ajaxSetup() to catch every call that gets a 401 and redirect to the login page. It looks like this,
$.ajaxSetup({
xhrFields : {withCredentials : true},
crossDomain : true,
complete : function(jqXHR, textStatus){
console.log("Default Test");
console.log(jqXHR);
console.log(textStatus);
if(jqXHR.status == 401){
console.log("True");
window.location.href = "login.html";
}else if(jqXHR.status == 423){
console.log("False");
window.location.href = "gateway.html";
}
}
});
I make a call that is unauthorized. When looking at it in fiddler, I get a 401 status. When I pull up the console in chrome, it also says that I got a 401 status. But, if I look at the jqXHR.status, it says it got 0 as a status. And text status only says, "error". Not very helpful. I've tested this in chrome, FF and IE. They all do the same thing.
Any idea how I can get it to show 401? I don't want to just do a,
if(jqXHR.status == 0){
window.location.href = "login.html";
}
Because I'm pretty sure that the status code of 0 will come up on more than just unauthorized calls, and I don't want to be redirecting to login when that's not the problem.
An example of one of the ajax calls that's being made.
function getStats(){
$.ajax({
url: DONEP+'dashboard/stats/',
type: "GET",
dataType: "json",
success: function(data){
...
}
});
}
Any help would be greatly apreciated! Thanks.