快速學(xué)習(xí)AngularJs HTTP響應(yīng)攔截器
任何時候,如果我們想要為請求添加全局功能,例如身份認(rèn)證、錯誤處理等,在請求發(fā)送給服務(wù)器之前或服務(wù)器返回時對其進(jìn)行攔截,是比較好的實現(xiàn)手段。
angularJs通過攔截器提供了一個從全局層面進(jìn)行處理的途徑。
四種攔截器
實現(xiàn) request 方法攔截請求
request: function(config) { // do something on request success return config || $q.when(config); }
該方法會在 $http 發(fā)送請求后臺之前執(zhí)行,因此你可以修改配置或做其他的操作。該方法接收請求配置對象(request configuration object)作為參數(shù),然后必須返回配置對象或者 promise 。如果返回?zé)o效的配置對象或者 promise 則會被拒絕,導(dǎo)致 $http 調(diào)用失敗。
實現(xiàn) requestError 方法攔截請求異常
requestError: function(rejection) { // do something on request error return $q.reject(rejection); }
有時候一個請求發(fā)送失敗或者被攔截器拒絕了,請求異常攔截器會俘獲那些被上一個請求攔截器中斷的請求。它可以用來恢復(fù)請求或者有時可以用來撤銷請求之前所做的配置,比如說關(guān)閉進(jìn)度條,激活按鈕和輸入框什么之類的。
實現(xiàn) response 方法攔截響應(yīng)
response: function(response) { // do something on response success return response || $q.when(response);}
該方法會在 $http 接收到從后臺過來的響應(yīng)之后執(zhí)行,因此你可以修改響應(yīng)或做其他操作。該方法接收響應(yīng)對象(response object)作為參數(shù),然后必須返回響應(yīng)對象或者 promise。響應(yīng)對象包括了請求配置(request configuration),頭(headers),狀態(tài)(status)和從后臺過來的數(shù)據(jù)(data)。如果返回?zé)o效的響應(yīng)對象或者 promise 會被拒絕,導(dǎo)致$http 調(diào)用失敗。
實現(xiàn) responseError 方法攔截響應(yīng)異常
responseError: function(rejection) { // do something on response error return $q.reject(rejection); }
有時候我們后臺調(diào)用失敗了,也有可能它被一個請求攔截器拒絕了或者被上一個響應(yīng)攔截器中斷了。在這種情況下,響應(yīng)異常攔截器可以幫助我們恢復(fù)后臺調(diào)用。
攔截器核心
攔截服務(wù)工廠
var app = angular.module("ajaxHttp", []); app.factory("httpInterceptor", [ "$q", "$rootScope", function($q, $rootScope) { return { request: function(config) { // do something on request success return config || $q.when(config); }, requestError: function(rejection) { // do something on request error return $q.reject(rejection) }, response: function(response) { // do something on response success return response || $q.when(response); }, responseError : function(rejection) { // do something on response error return $q.reject(rejection); } }; }]);
注冊攔截工廠方法
app.config(["$httpProvider", function($httpProvider) { $httpProvider.interceptors.push("httpInterceptor"); }]);
示例
對401,404的攔截處理
app.config(["$httpProvider", function($httpProvider) { $httpProvider.interceptors.push('httpInterceptor'); }]); app.factory("httpInterceptor", ["$q", "$injector", function($q, $injector) { return { "responseError": function(response) { if (response.status == 401) { var rootScope = $injector.get('$rootScope'); var state = $injector.get('$rootScope').$state.current.name; rootScope.stateBeforLogin = state; rootScope.$state.go("login"); return $q.reject(response); } else if (response.status === 404) { console.log("404!"); return $q.reject(response); } } }; ]);
以上內(nèi)容給大家分享快速學(xué)習(xí)AngularJs HTTP響應(yīng)攔截器 的相關(guān)知識,希望大家喜歡,同時感謝大家一直以來對腳本之家網(wǎng)站的支持。
相關(guān)文章
Angular限制input框輸入金額(是小數(shù)的話只保留兩位小數(shù)點)
最近做項目遇到這樣的需求輸入框要求輸入金額,只能輸入數(shù)字,可以是小數(shù),必須保留小數(shù)點后兩位。下面分為兩部分代碼給大家介紹實現(xiàn)代碼,需要的的朋友參考下吧2017-07-07詳解JavaScript的AngularJS框架中的表達(dá)式與指令
這篇文章主要介紹了JavaScript的AngularJS框架中的表達(dá)式與指令,文中羅列了幾個常用的指令屬性加以說明,需要的朋友可以參考下2016-03-03詳解封裝基礎(chǔ)的angular4的request請求方法
這篇文章主要介紹了詳解封裝基礎(chǔ)的angular4的request請求方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06利用Angularjs和bootstrap實現(xiàn)購物車功能
在學(xué)習(xí)了如何簡單開始一個Angular程序之后,跟著網(wǎng)上的教程我也來實現(xiàn)一個購物車功能,為了減少頁面樣式設(shè)計我使用了bootstrap來偷懶,現(xiàn)在分享給大家,有需要的可以參考借鑒。2016-08-08