mardi 26 juillet 2016

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

In my controller:

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

I set the HttpStatus to BAD_Request and this controller returns

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

This is my Ajax code :

           $.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);
            },
       });

However, HttpStatus.BAD_REQUEST is catched by "success" instead of "error". (It went inside the success block). Am I missing anything here?




Aucun commentaire:

Enregistrer un commentaire