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

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -