问题
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.
回答1:
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
回答2:
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:
来源:https://stackoverflow.com/questions/7867021/mootools-how-to-get-redirected-if-ajax-request-returns-redirect-code