Javascript 如何在所有$http调用中设置$http.error()?
我将如何为$http服务中的所有.error调用(无论在何处使用)设置函数 在我的整个应用程序中,我使用了许多封装在服务中的$http调用,并希望避免重复实现所有.error方法 e、 gJavascript 如何在所有$http调用中设置$http.error()?,javascript,angularjs,service,error-handling,Javascript,Angularjs,Service,Error Handling,我将如何为$http服务中的所有.error调用(无论在何处使用)设置函数 在我的整个应用程序中,我使用了许多封装在服务中的$http调用,并希望避免重复实现所有.error方法 e、 g 您可以创建和侦听器来捕获所有错误 像这样的 var applicationWideInterceptors = function ($q, toastrSrvc) { return { responseError:function(rejection) { //
您可以创建和侦听器来捕获所有错误 像这样的
var applicationWideInterceptors = function ($q, toastrSrvc) {
return {
responseError:function(rejection) {
//do something with the rejection
if (rejection.status === 404) {
toastrSrvc.error(rejection.data);
}
return $q.reject(rejection);
}
}
};
app.factory("appWideIntercpt", ["$q", "toastrSrvc", applicationWideInterceptors]);
别忘了将其添加到配置块中
$httpProvider.interceptors.push("appWideIntercpt"
一种可能的方法是设置自己的方法。
$httpProvider.interceptors.push("appWideIntercpt"