javascript - 来自 http 409 的 Ajax 解析响应

标签 javascript jquery ajax error-handling xmlhttprequest

尝试使用 Ajax 解析出消息对象,但无法获得对 messages.msg 的引用。它确实使用 PostMan 正确显示,但无法在 Ajax 中获取此消息。

PostMail 正确处理响应:

    {
       "httpStatusCode": 409,
       "httpStatus": "CONFLICT",
       "messages": {
           "msg": "ticket 1112 is owned by 2223"
    }
}

Ajax
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 接受
done(function(data, statusText, xhr) {
        console.log('SUCCESS');
        alert(data.messages.msg);
    })

最佳答案

必须捕获 409,然后添加到正文:
捕捉(HttpClientErrorException ex){
返回

ResponseEntity.status(HttpStatus.CONFLICT).body(ex.getResponseBodyAsString()); –

然后在 .fail 中访问
var 数据 = JSON.parse(jqXHR.responseText);

关于javascript - 来自 http 409 的 Ajax 解析响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60568373/

相关文章:

javascript - 表体未正确附加

javascript - 如何通过函数向类提供选项列表?

javascript - 如何通过 selenium 驱动程序从 "<div class=' ui-helper-hidden-accessible'>"中提取选项?

javascript - Vuejs ajax 调用不将更改映射到基础 html 表

javascript - 动态添加图像到列表元素

.net - WCF JSON 服务或 HTTPHandler

javascript - 循环中创建二维数组

javascript - 我想提交一个表单,但在提交之前我想从模态表单中获取一个值

javascript - 表单(Ajax)在 KendoUI 模板内发布

javascript - 更新 MYSQL 表给出成功消息,但不更新表