How to catch the expected (and supposed) 302 Ajax answer?

So, if you look back at my previous question about Exchange Autodiscover, you'll see that an easy way to get the Autodiscover URL is a secure, non-authenticated GET request to the server, ala:

http://autodiscover.exchangeserver.org/autodiscover/autodiscover.xml

The server will respond with a 302 redirect with the correct URL in the Location header.

First, I try to do something really simple with the Chrome extension, where I have:

 if (req.readyState==4 && req.status==302) { return req.getResponseHeader("Location"); } 

With another ajax call configured with full XML mail and user credentials,

But instead, Chrome freezes at this point, and a look at the developer's dashboard shows that it does not return a response, but instead acts like no answer, showing

 Uncaught Error: NETWORK_ERR: XMLHttpRequest Exception 101 

in the error log.

The way I see this, referring to the exact status of the response, is about the same as catching it, but I'm not sure if there is a problem with Chrome / WebKit or if that is how XHR requests always handle redirects.

I am not sure how to catch this so that I can get the headers from the answer.

Or is it possible to set an additional XHR so that when it receives 302 it sends a completely different request?

Quick update

I just changed it so that it does not check the response code:

 if (req.readyState==4) { return req.getResponseHeader("Location"); } 

and instead, when I warn it is null . and still there is the same error and lack of response in the dev console. SO does it look like it either does not track responses 302 as answers, or does something happen after you clear this answer?

+4
source share
1 answer

See the w3c docs on xhr : XHR will either transparently follow the redirect, or return a network error (for example, what you see from Chrome), depending on whether the redirect address is in the same origin as the original request. So your code really has no way to catch the 302 answer at all. Therefore, the response header "Location" is not set: at the moment you are checking the headers of the final answer, not the answer 302.

+3
source

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


All Articles