我在尝试实现
AJAX Spinner加载代码时出于不明原因获取此错误.
我不明白应该在哪里定义标头.我做了console.log(config),但我可以看到header:accept:text / html值.
以下是我的代码:
/** * Spinner Service */ //Spinner Constants diary.constant('START_REQUEST','START_REQUEST'); diary.constant('END_REQUEST','END_REQUEST'); //Register the interceptor service diary.factory('ajaxInterceptor',['$injector','START_REQUEST','END_REQUEST',function ($injector,START_REQUEST,END_REQUEST) { var $http,$rootScope,myAjaxInterceptor = { request: function (config) { $http = $http || $injector.get('$http'); if ($http.pendingRequests.length < 1) { console.log(config); $rootScope = $rootScope || $injector.get('$rootScope'); $rootScope.$broadcast(START_REQUEST); } } }; return myAjaxInterceptor; }]); diary.config(['$httpProvider',function ($httpProvider) { $httpProvider.interceptors.push('ajaxInterceptor'); }]);
解决方法
我想我有解决方案.
我在AngularJS项目中遇到了同样的问题,其中拦截器与你的拦截器完全相同(https://docs.angularjs.org/api/ng/service/$http#interceptors)
要缩短,拦截器会捕获配置并且必须返回它.而你忘记了.
那就是:
request: function (config) { $http = $http || $injector.get('$http'); if ($http.pendingRequests.length < 1) { $rootScope = $rootScope || $injector.get('$rootScope'); $rootScope.$broadcast(START_REQUEST); } return config; }