我似乎不能得到$httpProvider.interceptors来实际拦截.我在JSFiddle上创建了一个示例,它在拦截器运行时和$http响应成功时记录.请求拦截器在响应已经返回成功后运行.这似乎有点落后
原文链接:https://www.f2er.com/angularjs/143153.html我不能使用transformRequest,因为我需要修改配置中的参数.样品中未显示该部分.
我使用AngularJS 1.1.5
http://jsfiddle.net/skeemer/K7DCN/1/
使用Javascript
var myApp = angular.module('myApp',[]); myApp.factory('httpInterceptor',function ($q) { return { request: function (config) { logIt('- Modify request'); return config || $q.when(config); } }; }); myApp.config(function ($httpProvider) { $httpProvider.interceptors.push('httpInterceptor'); }); function MyCtrl($scope,$http) { // Hit a server that allows cross domain XHR $http.get('http://server.cors-api.appspot.com/server?id=8057313&enable=true&status=200&credentials=false') .success(function (data) { //logIt(data[0].request.url); logIt('- GET Successful'); }); $scope.name = 'Superhero'; } // Just the logging var logs = document.getElementById('logs'); function logIt(msg) { var e = document.createElement('div'); e.innerHTML = msg; logs.insertBefore(e,logs.firstChild); }
HTML
<div ng-controller="MyCtrl">Hello,{{name}}!</div> <br/> <div id="logs"></div>