我同意responseHeaders()
函数仅返回响应的头部信息,但您可以自定义它,这非常有用。
1.
为了解决您的问题,请使用以下代码:(其中$$service
是我的$resource实例。)
var serve = new $$service();
serve.id = "hello_wrongPath";
serve.$get()
.then(function (data) {
console.log("~~~hi~~~");
console.log(data);
return data;
})
.catch(function (error) {
console.log("~~~error~~~");
console.log(error);
console.log(error.status);
console.log(error.statusText);
console.log(error.config.timeout);
console.log(error.config.method);
console.log(error.config.url);
console.log(error.headers("content-type"));
return error.$promise;
})
.finally(function(data){
console.log("~~~finally~~~");
console.log(data);
});
如果您想在错误响应中仅捕获status,statusText,timeout,method,headers(与responseHeaders相同)
,则可以使用此方法。
2.
如果您想要在成功响应中查看响应详情,可以使用以下拦截器:
ng.module("baseInterceptor", [])
.factory("baseInterceptor", ["$q", function ($q) {
return {
'request': function (config) {
console.info(config);
config.timeout = 5000;
return config;
},
'requestError': function (rejection) {
console.info(rejection);
return $q.reject(rejection);
},
'response': function (response) {
console.log("~~interceptor response success~~");
console.log(response);
console.log(response.status);
console.log(response.config.url);
return response;
},
'responseError': function (rejection) {
console.log("~~interceptor response error~~");
console.log(rejection);
console.log(rejection.status);
return $q.reject(rejection);
}
};
}]);
然后将拦截器添加到模块中:
.config(["$httpProvider", function ($httpProvider) {
$httpProvider.interceptors.push("baseInterceptor");
}])