You can intercept responses by adding an interceptor to $httpProvider.interceptors using Angular 1.1.4+ (see documentation for finding interceptors here ).
For a certain type of content, such as json, you can reject the changes or throw an exception, even if the call was successful. You can change response.data , which will also be passed to your controller code:
myModule.factory('myHttpInterceptor', function ($q) { return { response: function (response) { // do something on success if(response.headers()['content-type'] === "application/json; charset=utf-8"){ // Validate response, if not ok reject var data = examineJSONResponse(response); // assumes this function is available if(!data) return $q.reject(response); } return response; }, responseError: function (response) { // do something on error return $q.reject(response); } }; }); myModule.config(function ($httpProvider) { $httpProvider.interceptors.push('myHttpInterceptor'); });
NOTE. Here is the original answer for versions prior to version 1.1.4 ( responseInterceptors are deprecated with Angular 1.1.4):
There may be a better way, but I think you can do something similar to this post using an HTTP interceptor (described here ) (for a specific type of content like json) where you potentially reject the changes or throw an exception , even though the challenge was successful. You can change the response.data , which will also be passed to your controller code.
myModule.factory('myHttpInterceptor', function ($q) { return function (promise) { return promise.then(function (response) { // do something on success if(response.headers()['content-type'] === "application/json; charset=utf-8"){ // Validate response if not ok reject var data = examineJSONResponse(response); // assumes this function is available if(!data) return $q.reject(response); } return response; }, function (response) { // do something on error return $q.reject(response); }); }; }); myModule.config(function ($httpProvider) { $httpProvider.responseInterceptors.push('myHttpInterceptor'); });
Gloopy Aug 14 '12 at 17:38 2012-08-14 17:38
source share