首页 > 代码库 > [AngularJS] Using AngularJS interceptors with $http
[AngularJS] Using AngularJS interceptors with $http
Sometimes you might need to modify HTTP requests and responses. This could be for a variety of reasons such as adding global logic handling for HTTP errors. With interceptors, you can easily accomplish this in your Angular applications.
var interceptor = function ($q, $location) { return { request: function (config) { console.log(config); return config; }, response: function (result) { console.log(‘Repos:‘); result.data.splice(0, 10).forEach(function (repo) { console.log(repo.name); }) return result; }, responseError: function (rejection) { console.log(‘Failed with‘, rejection.status, ‘status‘); if (rejection.status == 403) { $location.url(‘/login‘); } return $q.reject(rejection); } }};angular.module(‘app‘, []) .config(function ($httpProvider) { $httpProvider.interceptors.push(interceptor); }) .run(function ($http) { $http.get(‘https://api.github.com/users/bclinkinbeard/reposefw‘); });
In a lot of cases, interceptor can be used for Auth.
[AngularJS] Using AngularJS interceptors with $http
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。