As I ask in the title, how can I access the status code of the response in the done callback of an Ajax request?
var isUsernameGood = new Promise(
function(resolve, reject) {
console.log("Starting username check");
$.ajax({
type: "POST",
url: '/username',
contentType:'application/json',
data: JSON.stringify({username: document.getElementById("username").value})
})
.done(function(data, textStatus, xhr){
console.log("done");
console.log(data); console.log(textStatus); console.log (xhr);
resolve(xhr.status);
})
.fail(function(data, textStatus, xhr){
console.log("fail");
console.log(data); console.log(textStatus); console.log (xhr);
reject(xhr.status);
});
}
);
EDIT: The log prints "fail" and then "Object { readyState: 4, getResponseHeader: getResponseHeader(), getAllResponseHeaders: getAllResponseHeaders(), setRequestHeader: setRequestHeader(), overrideMimeType: overrideMimeType(), statusCode: statusCode(), abort: abort(), state: state(), always: always(), catch: catch(), … }
", then "error" and "unknown" althought in the Network tab of the inspect feature of the browser I see the request and also the response with its status code.
I'm noticing that in the Object corresponding to printing "data" in fail callback there are these fields:
i.e. it seems strangely that the status code is inside the "state" field rather than statusCode one. Do I have to access it from "state"?