Ajax可以't捕获从ResponseEntity(Spring MVC)对象发送的错误

Ajax can't catch error sent from the ResponseEntity (Spring MVC) object

本文关键字:MVC Spring 对象 错误 ResponseEntity 可以 Ajax      更新时间:2023-09-26

在我的控制器中:

return new ResponseEntity<>("Order is Fulfilled Already!", HttpStatus.BAD_REQUEST);

我将HttpStatus设置为BAD_Request,此控制器返回

{"headers":{},"body":"Order is Fulfilled Already!","statusCode":"BAD_REQUEST"}

这是我的Ajax代码:

           $.ajax({
            type : "GET",
            contentType : "application/json; charset=utf-8",
            url : "/tt/fulfill/order/${orderID}",
            dateType : 'json',
            cache: false,
            success: function (data) {
                alert(data.statusCode);
            },
            error: function (data) {
                alert(data.body);
            },
       });

然而,HttpStatus.BAD_REQUEST被"成功"而不是"错误"所吸引。(它进入了成功的区块)。我是不是遗漏了什么?

但是,HttpStatus.BAD_REQUEST被"success"而不是"错误"。

这意味着ajax是成功的ajaxerror在ajax失败时发生。

ajax错误函数的执行主要是因为4xx&5xx发布

您可以检查此ajax的状态代码。