我正在使用Angular Interceptor,我希望从500个错误中获取消息(内部服务器错误)。
问题是,我在Interceptor内的 responseError 中的 rejection.data 中获取了整个HTML(截图如下)。
我读到我必须配置 web.config ,但我仍然获得整个HTML。我只是想收到消息。
有可能吗?
角度拦截器:
pyodbc
的Web.Config
app.config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push(function ($q, $rootScope) {
return {
request: function (config) {
//the same config / modified config / a new config needs to be returned.
return config;
},
requestError: function (rejection) {
//Initializing error list
if ($rootScope.errorList == undefined) {
$rootScope.errorList = [];
}
$rootScope.errorList.push(rejection.data);
//It has to return the rejection, simple reject call doesn't work
return $q.reject(rejection);
},
response: function (response) {
//the same response/modified/or a new one need to be returned.
return response;
},
responseError: function (rejection) {
//Initializing the error list
if ($rootScope.errorList == undefined) {
$rootScope.errorList = [];
}
//Adding to error list
$rootScope.errorList.push(rejection.data);
//It has to return the rejection, simple reject call doesn't work
return $q.reject(rejection);
}
};
});
}]);
编辑: 我想从异常快照中获取消息
答案 0 :(得分:1)
我想从500错误中获取消息(内部服务器错误)。
使用response.statusText
获取消息:
responseError: function (errorResponse) {
//Initializing the error list
if ($rootScope.errorList == undefined) {
$rootScope.errorList = [];
}
//Adding to error list
$rootScope.errorList.push(errorResponse.statusText);
//It has to return the rejection, simple reject call doesn't work
return $q.reject(errorResponse);
}
来自文档:
响应对象具有以下属性:
- data -
{string|Object}
- 使用转换函数转换的响应主体。- status -
{number}
- 响应的HTTP状态代码。- 标题 -
{function([headerName])}
- 标题获取功能。- config -
{Object}
- 用于生成请求的配置对象。- statusText -
{string}
- 响应的HTTP状态文本。