Tôi dường như không thể nhận được $ httpProvider.interceptors để thực sự chặn. Tôi đã tạo một mẫu trên JSFiddle ghi nhật ký khi trình chặn chặn được chạy và khi phản hồi $ http thành công. Trình chặn yêu cầu được chạy sau khi phản hồi đã được trả về thành công. Điều này có vẻ hơi lạc hậu.
Tôi không thể sử dụng transformRequest vì tôi cần thay đổi các tham số trong cấu hình. Phần đó không được hiển thị trong mẫu.
Tôi đang sử dụng 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>
Wow ... cảm thấy câm bây giờ. Cảm ơn, mặc dù! Đã tìm ra mã khác của tôi rằng tôi đã sử dụng $ httpProvider.responseInterceptors không được chấp nhận thay vì $ httpProvider.interceptors. – skeemer