gpt4 book ai didi

jQuery .error jqXHR.responseText 为空

转载 作者:行者123 更新时间:2023-12-03 22:22:53 25 4
gpt4 key购买 nike

我有一个可以引发异常的 Java Spring MVC Controller 。我有一个 @ExceptionHandler 设置来处理这些错误,我想用它来将异常的消息返回给调用者。

服务器代码是:

    @ExceptionHandler(DeviceException.class)
@ResponseStatus(HttpStatus.METHOD_FAILURE)
@ResponseBody
public String handleException(DeviceException ex) {
return ex.getMessage();
}

我已经测试了抛出 DeviceException 并使用 CLI 中的 Curl 获取结果,异常的消息在响应正文中返回。我无法让 jQuery .error 处理程序显示它,但是,我的处理程序代码是:

            error: function(jqXHR, textStatus, errorThrown) {
// problem, the data is always blank
alert('jqXHR.responseText = ', jqXHR.responseText);
}

我得到的responseText只是一个空字符串。如何让 @ExceptionHandler 返回的字符串显示在 jQuery 错误处理程序中?

最佳答案

尝试这样的事情:

var msg = '<div style="padding:20px;">';
msg += "We're sorry, there was an error during your request. Please refresh the page, then try again. If your problem persists, please contact technical support. <br /><br /> <b>Cause:</b> <br /> ";
switch (textStatus) {
case ('timeout'):
msg += 'Request timed out.';
break;
case ('error'):
msg += 'Error status returned.';
break;
case ('abort'):
msg += 'Request was aborted.';
break;
case ('parseerror'):
msg += 'There was an error parsing your request.';
break;
default:
msg += 'No status returned.';
break;
}
$(this).html(msg + ' <br /><br /> ' + errorThrown + ' <br /></div>');

关于jQuery .error jqXHR.responseText 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6850760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com