[英]Redirect after POST using the Location header with jQuery
我想使用jQuery 1.7的Location
标头重定向到目标。
我的代码看起来像这样
$('#creationLink').click(function(){
$.ajax({
type: 'POST',
url: '/',
success: function(data, textStatus, xhr) {
window.location = xhr.getResponseHeader("Location");
}
})
});
......但它不起作用。 xhr.getResponseHeader("Location")
为空。
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
如何使用位置标题重定向?
AFAIK,浏览器应该在XHR期间透明地跟随响应头中的重定向。 也就是说,XHR实际上会查看响应,查看Location标头,然后继续神奇地运行该URI的第二个请求。 只有当它的这个结果必给你任何东西,以及它给你的第二个请求的结果。
看到 这个stackoverflow答案 !
因此,如果您需要重定向功能,则必须使您请求的内容以其他方式返回目标URI,例如作为JSON响应。
请参阅 此stackoverflow解决方案 !
PS。 参考: http : //www.w3.org/TR/XMLHttpRequest/#infrastructure-for-the-send-method
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.