Mootools:如果ajax请求返回重定向代码,如何重定向?
如果请求返回 302 或 303,我需要重定向用户。似乎没有标准方法可以做到这一点(奇怪,我认为这是一个很常见的任务)。
我有这样的代码:
var request = new Request({
url: "/some-url/",
method: 'get',
evalScripts: true,
onComplete: function(){
console.log(this.status);
}
});
控制台打印 0 状态,但 Web 检查器显示状态代码为 302 Found,因此我无法手动检查代码并重定向。有人知道我做错了什么吗?
谢谢。
I need to redirect user if request returns 302 or 303. It seems that there isn't a standard way to do this (odd, I thought it's quite a common task).
I have this code:
var request = new Request({
url: "/some-url/",
method: 'get',
evalScripts: true,
onComplete: function(){
console.log(this.status);
}
});
Console prints 0 status but web inspector shows that the status code is 302 Found, so I can't manually check the code and get redirected. Anyone know what I'm doing wrong?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,对 301/302/303 等的 ajax 请求将遵循新的
location
并根据规范重新发出请求。如果您没有看到第二个请求,则说明有问题,例如无限循环或类似的...更多信息:http://dev.w3.org/2006/webapi/XMLHttpRequest/#infrastruct-for-the-send-method
normally, an ajax request to a 301/302/303 etc will follow the new
location
and re-issue the request as per specification. if you are not seeing a second request, something is wrong, like an infinite loop or similar...more here: http://dev.w3.org/2006/webapi/XMLHttpRequest/#infrastructure-for-the-send-method
我已经尝试了两种情况(302 和 303),它遵循规范:用户代理自动重定向到在 Location: 响应标头中指定的 url。所以,最后console.log(this.status)确实返回
200
,但不用担心,因为重定向是由浏览器自动处理的(至少在FF和Chrome中)。这是 firebug 控制台:
I've tryed both cases (302 and 303) and it follows the spec: user agent automatically redirects to url, specified in the Location: header of response. So, finally console.log(this.status) indeed return
200
, but no worry because redirect is handled by browser automatically (at least in FF and Chrome).That is the firebug console: