XmlHttpRequest gets callback response when request fails

I have a simple request that I make for my service:

 var request = new XMLHttpRequest();
    request.open("OPTIONS", url, true);
    request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
    request.setRequestHeader('Accept', 'application/json');

    request.onreadystatechange = function () {
        if (request.readyState != 4) {
            return;
        }

        var authType = request.getResponseHeader("WWW-Authenticate");

        makeRequest(...);
    };

    request.send();
}

So what I'm trying to achieve is to make a call to my endpoint to find out what type of authorization (base or carrier), and then when I get the type auth, I will make the actual request with the appropriate credentials.

If I make the request manually, I get 401 unauthorized, which is fine, but I also get WWW-Authenticate: Basic ... in my headers. However, if I make this javascript call, it just fails with 401: unauthorized, but I do not get this unsuccessful response in my callback, so authType will be undefined.

javascript, , , ?

+4
1

XMLHttpRequest onerror, :

var request = new XMLHttpRequest();
...
request.onerror = function(){
    if (this.status == 401) {
    } 
} 
...
request.send();
0

Source: https://habr.com/ru/post/1613196/


All Articles