-2

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:

enter image description here

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"?

M-elman
  • 313
  • 5
  • 16
  • Possible duplicate of [How to get response status code from jQuery.ajax?](https://stackoverflow.com/questions/5344145/how-to-get-response-status-code-from-jquery-ajax) – John Doe Nov 16 '17 at 16:03
  • @Phoenix2105 I have updated my question with new logs – M-elman Nov 16 '17 at 16:31
  • Look at your browser console. It's probably an error. Make sure errors are turned on in the filter. – Kevin B Nov 16 '17 at 16:40
  • Note that `data` is the XHR. it's mostly irrelevant to your situation other than for seeing what the status code is and reading the raw responseText. In your case it's showing you that the status code is 449. – Kevin B Nov 16 '17 at 16:43
  • 449 typically means you haven't provided the required information. – Kevin B Nov 16 '17 at 16:44
  • @KevinB it's not related to my question, but which status code would you choose to signal incorrect infos provided by client (e.g. already existing username or non-existing birthplace?) – M-elman Nov 16 '17 at 16:47
  • 1
    I would use bad request – Kevin B Nov 16 '17 at 16:48
  • Anyway I managed to access the status code from xhr.status in done callback and textStatus.status in the fail one. Only one more doubt: why if I try to print xhr in the fail callback I get "undefined"? – M-elman Nov 16 '17 at 16:49
  • Dunno. it shouldn't be undefined if you're directly printing the xhr (which is data in your case) – Kevin B Nov 16 '17 at 16:50

1 Answers1

-1

There are several ways to access the response from an ajax call, see for example: Get Http Status Code from an ajax response with jquery Hope this helps!

  • I tried using that structure but I get undefined when I try to print out the value of xhr.status – M-elman Nov 16 '17 at 16:14