使用Location头使用jQuery POST后重定向 [英] Redirect after POST using the Location header with jQuery

查看:172
本文介绍了使用Location头使用jQuery POST后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用位置头使用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。参考:<一href="http://www.w3.org/TR/XMLHtt$p$pquest/#infrastructure-for-the-send-method">http://www.w3.org/TR/XMLHtt$p$pquest/#infrastructure-for-the-send-method

这篇关于使用Location头使用jQuery POST后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆