我似乎无法让$ httpProvider.interceptors实际拦截.我在JSFiddle上创建了一个示例,它在拦截器运行时以及$ http响应成功时记录.请求拦截器在响应已成功返回后运行.这看起来有点倒退.
我无法使用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 …Run Code Online (Sandbox Code Playgroud) angularjs ×1