欢迎来到代码驿站!

AngularJS

当前位置:首页 > 网页前端 > AngularJS

AngularJS中的拦截器实例详解

时间:2021-05-08 09:04:44|栏目:AngularJS|点击:

AngularJS中的拦截器实例详解

异步操作

有时候需要在拦截器中做一些异步操作。幸运的是, AngularJS 允许我们返回一个 promise 延后处理。它将会在请求拦截器中延迟发送请求或者在响应拦截器中推迟响应。

下面是项目中用到的代码。

ZbtjxcApp.factory('myHttpInterceptor', ['$q', '$window','$location', function($q, $window,$location) {
  return {
    // 全局响应
    'response': function(response) {
      // 这里还可以利用promise做异步处理,目前不用做,好像也能满足需求
      switch (response.status) {
        case (200):
          if (response.data) {
            //这里可以做自己相应的处理
            if (response.data.code == 100100) {
              $window.location.href = "/login.html";
            } 
            /*else if(response.data.code = 100200) {
              $location.path('/unauthorized');
            }*/
          }
          break;
        case (500):
          //后期在处理
          console.log("服务器正忙 -- 500");
          break;
        case (404):
          console.log("not found -- 404");
          break;
        default:
          console.log("服务器正忙");
      }
      return response;
    }
  };
}]).config(['$httpProvider', function($httpProvider) {
  $httpProvider.interceptors.push('myHttpInterceptor');
}]);

ZbtjxcApp.factory('pageService', ['$http', function($http) {
  var getPageList = function(geturl, getdata) {
    return $http.get(geturl, {
      params: getdata
    });
  }
  return {
    getPageList: getPageList
  };
}]);


感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:AngularJs bootstrap搭载前台框架――准备工作

栏    目:AngularJS

下一篇:如何利用AngularJS打造一款简单Web应用

本文标题:AngularJS中的拦截器实例详解

本文地址:http://www.codeinn.net/misctech/116880.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有