Redirecting after POST using Location header using jQuery

I want to redirect to the target using the Location header with jQuery 1.7.

My code is as follows

 $('#creationLink').click(function(){ $.ajax({ type: 'POST', url: '/', success: function(data, textStatus, xhr) { window.location = xhr.getResponseHeader("Location"); } }) }); 

... but it does not work. xhr.getResponseHeader("Location") is null.

HTTP headers:

 POST / HTTP/1.1 Host: localhost:9000 X-Requested-With: XMLHttpRequest Content-Length: 0 HTTP/1.1 302 Found Content-Type: text/plain; charset=utf-8 Location: http://localhost:9000/vIRdD0PdWp4/bearbeiten Content-Length: 0 

How can I redirect the use of the location header?

+4
source share
1 answer

AFAIK, browsers should, during XHR, transparently follow the redirection in the response header. That is, XHR will really look at the answer, see the Location header and start magically launching the second request for this URI. Only when he has a result will he give you anything at all, and what it gives you is the result of the second query.

See fooobar.com/questions/319651 / ... !

So, if you need a redirection function, you have to do what you request, return the target URI in some other way, for example. as a JSON response.

See fooobar.com/questions/1299 / ... !

PS. link: http://www.w3.org/TR/XMLHttpRequest/#infrastructure-for-the-send-method

+14
source

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