【发布时间】:2020-06-19 10:38:30
【问题描述】:
尝试使用 Ajax 解析出消息对象,但无法获得对 messages.msg 的引用。它确实使用 PostMan 正确显示,但无法在 Ajax 中获取此消息。
PostMail 正确处理响应:
{
"httpStatusCode": 409,
"httpStatus": "CONFLICT",
"messages": {
"msg": "ticket 1112 is owned by 2223"
}
}
阿贾克斯
fail(function(xhr, status, error) {
//fail(function(err) { did not work
//console.log(err); did not work
//xhr.responseText; did not work
//var msg = $.parseJSON(err).msg; did not work
//alert(msg); did not work
var acc = []
$.each(xhr, function(index, value) {
acc.push(index + ': ' + value);
});
alert(JSON.stringify(acc)); //did not see any object which includes response message
console.log('FAIL');
})
完成显示非常适合 202 ACCEPTED
done(function(data, statusText, xhr) {
console.log('SUCCESS');
alert(data.messages.msg);
})
【问题讨论】:
-
你能添加完整的代码如何调用失败和完成方法
-
必须捕获 409,添加到正文并访问 .fail var data = JSON.parse(jqXHR.responseText);
-
catch (HttpClientErrorException ex) { return ResponseEntity.status(HttpStatus.CONFLICT).body(ex.getResponseBodyAsString());
标签: javascript jquery ajax error-handling xmlhttprequest