Javascript AngularJS拦截器仅用于GET请求
我正试图使用拦截器来拦截我的ajax请求,但是它只适用于Javascript AngularJS拦截器仅用于GET请求,javascript,angularjs,fullcalendar,interceptor,Javascript,Angularjs,Fullcalendar,Interceptor,我正试图使用拦截器来拦截我的ajax请求,但是它只适用于GET请求,我使用POST和PUT尝试了它,但注意要做到这一点。有什么想法吗? 下面是我创建拦截器的方法 app.factory('httpInterceptor', ['$q', '$location', '$injector', function ($q, $location, $injector) { return { request: function(config) { var Au
GET
请求,我使用POST
和PUT
尝试了它,但注意要做到这一点。有什么想法吗?下面是我创建拦截器的方法
app.factory('httpInterceptor', ['$q', '$location', '$injector',
function ($q, $location, $injector) {
return {
request: function(config) {
var AuthService = $injector.get('AuthService');
console.log(AuthService.getCurrentUser(), AuthService.getCurrentUser().key);
if(config.usingKey === true) config.headers['X-API-KEY'] = AuthService.getCurrentUser().key;
if(config.beforeSend && typeof config.beforeSend == "function") config.beforeSend();
return config;
}
};
}]);
我正在用这些服务打电话
angular.module('myapp')
.service('EventService', ['$filter', '$http', 'CONFIG', function ($filter, $http, CONFIG) {
this.create = function(params, beforeSend, usingkey) {
if(usingkey == null) usingkey = true;
return $http.put(CONFIG.DEV ? CONFIG.URL_API_DEV + "event" : CONFIG.URL_API + "event", {params: params, beforeSend: beforeSend, usingKey: usingkey})
};
}]);
最后,我用这个代码调用我的服务
select: function (start, end, jsEvent, view, resourceObj) {
EventService.create({}, function(){
$('.status').find('i').removeClass("fa-check").addClass("fa-spin fa-circle-o-notch");
}).then(function($data){
$('.status').find('i').removeClass("fa-spin fa-circle-o-notch").addClass('fa-check');
event.realid = $data.data.value.id;
Calendar.fullCalendar('updateEvent', event);
}, function(err){
$('.status').find('i').removeClass("fa-spin fa-circle-o-notch").addClass('fa-close').css({'color': 'red'});
toastr.error(err);
});
$scope.$apply(); // The $apply is for another part of the code, it is required.
}
如果你有任何想法,请告诉我,
谢谢,祝你今天愉快
- Nic
$httpProvider.interceptors.push('httpInterceptor')代码>