javascript - How to set $http.error() in angular throughout all $http calls? -
how go setting function .error calls $http service wherever used.
throughout app have used many $http calls wrapped in service , avoid duplication of implementing .error methods.
e.g.
posttest: function (data) { var url = '/test'; return $http({ method: 'post', url: url, data: data }). success(function (data) { return data; }). error(function (status) { //avoid duplication of throughout $http calls. if (status === 404) {} return status; }); }
you create , interceptor catch errors
so
var applicationwideinterceptors = function ($q, toastrsrvc) { return { responseerror:function(rejection) { //do rejection if (rejection.status === 404) { toastrsrvc.error(rejection.data); } return $q.reject(rejection); } } }; app.factory("appwideintercpt", ["$q", "toastrsrvc", applicationwideinterceptors]);
dont forget add config block
$httpprovider.interceptors.push("appwideintercpt"
Comments
Post a Comment