I've got this factory
that I'm pushing into the $httpProvider
interceptors
我有这个工厂,我正在推进$ httpProvider拦截器
function httpErrorHandler($q) {
return {
'responseError': function (response) {
if (response.status === 500) {
if (response.data && response.data.html) {
var msg = angular.fromJson(response.data.html).Data;
showStatusMessage(msg, 'error'); // assume available
} else {
showStatusMessage(response.statusText, 'error');
}
}
// Always reject (or resolve) the deferred you're given
return $q.reject(response);
}
};
}
and the interceptor code:
和拦截器代码:
function config($httpProvider) {
$httpProvider.interceptors.push('HttpErrorHandler');
}
When I get the failed response from the server, looking in Chrome DevTools > Network tab, selecting the failed XHR and the Response tab, I get this:
当我从服务器收到失败的响应时,查看Chrome DevTools>网络选项卡,选择失败的XHR和响应选项卡,我得到:
{"Success":false,"Data":"Error Code 0000-229566."}
and what shows up in the status message is the response.statusText
而状态消息中显示的是response.statusText
To try to be complete, here are the Response Headers:
要尝试完成,请参阅响应标题:
Cache-Control:no-cache, no-store, must-revalidate
Content-Length:110
Content-Security-Policy:frame-ancestors 'self'
Content-Type:application/json; charset=utf-8
Date:Fri, 26 Aug 2016 16:26:11 GMT
Expires:-1
Pragma:no-cache
Strict-Transport-Security:max-age=31536000; includeSubDomains
X-Content-Type-Options:nosniff
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
What I'd like is to have the "Error Code 000-1234" show up in my status message, but it's not coming. How can I get that to show up?
我想要的是在我的状态信息中显示“错误代码000-1234”,但它不会出现。我怎么能让它出现?
1 个解决方案
#1
0
Instead of accessing the .Data property of the response content you are reading the statusText property of the response header. statusText seems like the description of the response's status code, more here.
您正在读取响应标头的statusText属性,而不是访问响应内容的.Data属性。 statusText看起来像响应的状态代码的描述,更多这里。
Scripted a working sample:https://embed.plnkr.co/xz54Mh2zGguHB0cLquGE/ My error message property is .status_message instead of .Data
脚本一个工作示例:https://embed.plnkr.co/xz54Mh2zGguHB0cLquGE/我的错误消息属性是.status_message而不是.Data
responseError: function (response) {
/*
NOTE: if "status_message" is not in the parsing string JSON.parse(response.data.html).status_message
will thrown an undefined error and break the execution
*/
var msg = !response.data ? 'Error occured ...'
: !response.data.html ? response.data.status_message : JSON.parse(response.data.html).status_message;
$alert.show(msg, 'danger');
return $q.reject(response);
}
#1
0
Instead of accessing the .Data property of the response content you are reading the statusText property of the response header. statusText seems like the description of the response's status code, more here.
您正在读取响应标头的statusText属性,而不是访问响应内容的.Data属性。 statusText看起来像响应的状态代码的描述,更多这里。
Scripted a working sample:https://embed.plnkr.co/xz54Mh2zGguHB0cLquGE/ My error message property is .status_message instead of .Data
脚本一个工作示例:https://embed.plnkr.co/xz54Mh2zGguHB0cLquGE/我的错误消息属性是.status_message而不是.Data
responseError: function (response) {
/*
NOTE: if "status_message" is not in the parsing string JSON.parse(response.data.html).status_message
will thrown an undefined error and break the execution
*/
var msg = !response.data ? 'Error occured ...'
: !response.data.html ? response.data.status_message : JSON.parse(response.data.html).status_message;
$alert.show(msg, 'danger');
return $q.reject(response);
}