【问题标题】:Ajax parsing response from http 409来自 http 409 的 Ajax 解析响应
【发布时间】: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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2021-09-28
    • 2016-04-14
    • 2011-04-30
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 2015-02-26
    • 1970-01-01
    相关资源
    最近更新 更多