您可以通过
$httpProvider.interceptors在Angular1.1.4+中添加拦截器来拦截响应(请参阅此处的文档以搜索拦截器)。
对于像json这样的特定内容类型,即使调用成功,您也可能拒绝更改或引发异常。您还可以在
response.data此处修改将传递给您的控制器代码的:
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');});注意: 这是1.1.4之前版本(
responseInterceptorsAngular 1.1.4不推荐使用)的原始答案:
也许有更好的方法,但我认为您可以使用http响应拦截器(在此介绍)(针对json等特定内容类型)执行类似于此帖子的操作,即使调用成功,您也可能拒绝更改或引发异常。您也可以在此处修改将传递到您的控制器代码的。
response.data
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');});


