Javascript Angularjs拦截器导致错误
我正在尝试为我的示例angular.js应用程序创建一个拦截器。这是我的配置代码:Javascript Angularjs拦截器导致错误,javascript,angularjs,Javascript,Angularjs,我正在尝试为我的示例angular.js应用程序创建一个拦截器。这是我的配置代码: .config(function ($routeProvider, $httpProvider) { $routeProvider .when('/', { templateUrl: 'views/main.html', controller: 'MainCtrl' }) .otherwise({ redirectTo: '/'
.config(function ($routeProvider, $httpProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.otherwise({
redirectTo: '/'
});
$httpProvider.interceptors.push('failedRequestInterceptor');
}))
我的拦截器呢
angular.module('sandboxApp').factory('failedRequestInterceptor', function ($q) {
return {
'response': function (config) {
console.log("test");
}
}
}))
它在我的控制台中显示“test”字符串,但很快我就得到了错误“response is undefined”,应用程序加载失败
也许这与使用yeoman及其附带的工具有关,下面是控制台中的确切错误文本:
Error: response is undefined handleRequestFn/<@http://localhost:9000/bower_components/angular/angular.js:16002:11 processQueue@http://localhost:9000/bower_components/angular/angular.js:13137:27 scheduleProcessQueue/<@http://localhost:9000/bower_components/angular/angular.js:13153:27 $RootScopeProvider/this.$get</Scope.prototype.$eval@http://localhost:9000/bower_components/angular/angular.js:14353:16 $RootScopeProvider/this.$get</Scope.prototype.$digest@http://localhost:9000/bower_components/angular/angular.js:14169:15 $RootScopeProvider/this.$get</Scope.prototype.$apply@http://localhost:9000/bower_components/angular/angular.js:14457:13 done@http://localhost:9000/bower_components/angular/angular.js:9614:36 completeRequest@http://localhost:9000/bower_components/angular/angular.js:9804:7 requestLoaded@http://localhost:9000/bower_components/angular/angular.js:9745:1
错误:响应未定义HandlerRequestfn/请遵循以下代码方法:
拦截器功能:
拦截器注入:
请删除“response”中的单引号。我已经尝试过了,谢谢它成功了,但有没有办法将拦截器移动到独立文件中?你可以将它放在myfile.js中,但请确保它的脚本标记在app.js上
var interceptor = function ($q, $location) {
return {
response: function (result) {
console.log(result);
return result;
}
}
};
angular.module('app', [])
.config(function ($httpProvider) {
$httpProvider.interceptors.push(interceptor);
});