Mootools: How to get redirected if ajax request returns redirect code?

China☆狼群 提交于 2019-12-24 11:04:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!