在Angularjs中处理$http.get和$http.post错误
各位 我在我的代码中使用了在Angularjs中处理$http.get和$http.post错误,angularjs,http,post,get,Angularjs,Http,Post,Get,各位 我在我的代码中使用了$http.get和$http.post。我有点不知所措,不知道如何以全球方式处理这些调用期间发生的错误。 目前我在每次通话中都有.success(doSomething).error(doSomething) 我想改变这一点,而只是在我的页面顶部显示错误 我读过关于在myapp.something中使用拦截器的文章。但我绝对不明白如何实施这一点 请协助angularjs应用程序在配置阶段连接响应拦截器。您可以使用它们全局响应任何$http请求。请注意,模板文件也使用$
$http.get
和$http.post
。我有点不知所措,不知道如何以全球方式处理这些调用期间发生的错误。
目前我在每次通话中都有.success(doSomething).error(doSomething)
我想改变这一点,而只是在我的页面顶部显示错误
我读过关于在myapp.something中使用拦截器的文章。但我绝对不明白如何实施这一点
请协助angularjs应用程序在配置阶段连接响应拦截器。您可以使用它们全局响应任何$http请求。请注意,模板文件也使用$http request,因此您可能希望将拦截器中的一些请求过滤为只需要响应的请求 要成功使用响应拦截器,需要对模式有很好的理解 以下是如何使用它们的示例:
angular.module('services')
.config(function($httpProvider){
//Here we're adding our interceptor.
$httpProvider.responseInterceptors.push('globalInterceptor');
})
//Here we define our interceptor
.factory('globalInterceptor', function($q){
//When the interceptor runs, it is passed a promise object
return function(promise){
//In an interceptor, we return another promise created by the .then function.
return promise.then(function(response){
//Do your code here if the response was successful
//Always be sure to return a response for your application code to react to as well
return response;
}, function(response){
//Do your error handling code here if the response was unsuccessful
//Be sure to return a reject if you cannot recover from the error somehow.
//This way, the consumer of the $http request will know its an error as well
return $q.reject(response);
});
}
});
注意!!此方法已被弃用。请按此链接查找备选方案,