处理 angularjs $http 中的重定向 [英] Handle redirects in angularjs $http

查看:51
本文介绍了处理 angularjs $http 中的重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用了 angularjs http 服务,我在 网站:

I'm using angularjs http service in my app, and I noticed this in the website:

如果 AJAX 调用成功(服务器在200和209),传递给success()函数的函数是执行.如果 AJAX 调用失败(除重定向),传递给 error() 方法的函数被执行.

If the AJAX call succeeds (the server sends back an HTTP code between 200 and 209), the function passed to the success() function is executed. If the AJAX call fails (all other codes except for redirects), the function passed to the error() method is executed.

显然,重定向状态代码不是成功的一部分,也不是错误回调.那么我们将如何处理重定向呢?

Obviously, the redirects status code isn't part of success neither error callbacks. So how are we going to handle redirects?

这就是我的脚本所做的,使用 http get 从服务器获取数据.如果会话过期,服务器返回302 状态码.我应该捕获该状态代码,然后将页面重定向到登录.

This is what my script does, get data from server using http get. If session expires, server returns 302 status code. I should catch that status code then redirect the page to login.

app.controller('GraphController', function($scope, $http, localStorageService, $interval) {
        $scope.GraphData = [{"key":"in","values":[]},{"key":"out","values":[]}]; 

        $scope.pollStats = function() {
            $http.get('/statistics/getStat').success(function(lastData, status, headers) {              
                if (status==302) {
                    //this doesn't work
                    //window.location=headers('Location');
                    //this doesn't work either
                    window.location.replace(headers('Location'));
                }else if (status==200) {
                    ...processData
                }
            });
        };
});

显然我的脚本无法工作,因为成功回调无法处理重定向.那么我们应该如何处理呢?

Obviously my script won't work because success callback can't handle redirects. So how are we supposed to handle that?

推荐答案

请注意,这是特定于我的项目.

Please note that this is specific to my project.

目标:捕获 302 状态代码并重定向页面(在我的情况下用于登录).

Goal: Catch 302 status code and redirect the page (to login in my case).

结果:在 firebug 中,我可以看到响应代码是 302,但后来我发现 angularjs 通过简单地打印状态值 (response.status) 返回 200.所以一开始你以为自己没救了.但就我而言,我所做的是获取数据 (response.data),查找只能在我的登录页面中找到的字符串.然后就是这样.问题解决了:D

Result: In firebug, I could see that the response code is 302 but then I found out that angularjs returns 200 by simply printing the value of status (response.status). So at first you'd thought that you're hopeless. But in my case, what I did was get the data (response.data), look for the string that could be only found in my login page. Then that's it. problem solved :D

这个想法是在这里取的.

The idea was taken here.

这是代码

app.factory('redirectInterceptor', function($q,$location,$window){
    return  {
        'response':function(response){
        if (typeof response.data === 'string' && response.data.indexOf("My Login Page")>-1) {
            console.log("LOGIN!!");
            console.log(response.data);
            $window.location.href = "/login.html";
            return $q.reject(response);
        }else{
            return response;
        }
        }
    }

    });

    app.config(['$httpProvider',function($httpProvider) {
        $httpProvider.interceptors.push('redirectInterceptor');
    }]); 

这篇关于处理 angularjs $http 中的重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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