使用Location头使用jQuery POST后重定向重定向、Location、jQuery、POST

2023-09-10 17:54:27 作者:血染墨冢

我想用位置头使用jQuery 1.7重定向到目标。

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

我的code看起来像这样

My code looks like this

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

...但它不工作。 xhr.getResponseHeader(位置)为空。

HTTP标头:

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 using the location header?

推荐答案

AFAIK,浏览器应该是,一个XHR时,透明地按照响应头重定向。也就是说,XHR实际上看的反应,看到Location头,并继续神奇地运行第二次请求该URI。只有当它的结果的是的将它给你任何东西,和它给你的是结果的的第二的要求。

AFAIK, browsers are supposed to, during an XHR, transparently follow the redirect in the response header. That is, the XHR will actually look at the response, see the Location header, and proceed to magically run a second request for that URI. Only when it has the result of that will it give you anything at all, and what it gives you is the result of the second request.

查看 这个计算器回答!

所以,如果你需要一个重定向功能,你必须让你请求的事返回目标URI在一些其他的方式,例如作为一个JSON响应。

So, if you need a redirect feature, you'll have to make the thing you request return the target URI in some other way, e.g. as a JSON response.

查看 这个计算器解决方案!

PS。参考:http://www.w3.org/TR/XMLHtt$p$pquest/#infrastructure-for-the-send-method