处理重定向在a​​ngularjs $ HTTP [英] Handle redirects in angularjs $http

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

问题描述

即时通讯使用我的应用程序angularjs HTTP服务。我在网站注意到了这一点。

Im using angularjs http service in my app. and I noticed this in the website

如果AJAX调用成功(在服务器发送HTTP code之间
  200和209),该函数传递到成功()函数是
  执行。如果AJAX调用失败(所有其他codeS除
  重定向),函数传递给错误()方法执行。

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.

显然,既不成功回调错误的重定向状态code心不是一部分。
那么我们如何去处理重定向?

obviously, the redirects status code isnt part of success neither error callbacks. So how are we going to handle redirects?

这是我的脚本做什么,让使用HTTP GET从服务器上的数据。如果会话过期,服务器返回302状态code。我要赶上状态code然后重定向的页​​面进行登录。

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 wont work because success callback cant handle redirects. So how are we supposed to handle that?

推荐答案

请注意,这是具体到我的项目。

Please note that this is specific to my project.

目标:捕捉302状态code和重定向的页​​面(在我的情况下登录)

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

结果:在Firebug,我可以看到,响应code是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.

这里的code

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');
    }]); 

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

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