admin管理员组

文章数量:1362438

SERVER SIDE: Spring Framework

I have a Spring Controller that has a method that returns the type ResponseEntity<String>.

For pletely good requests I return the following:

return new ResponseEntity<>(OK_MESSAGE, new HttpHeaders(), HttpStatus.OK);

But if there's any problem during the execution or Exception catching, I return:

return new ResponseEntity<>(ERROR_MESSAGE, new HttpHeaders(), HttpStatus.BAD_REQUEST);

Where ERROR_MESSAGE contains a customized String for each type of Exception catched.

CLIENT SIDE: AJAX call

When that POST method is called and returns HttpStatus.OK, AJAX

success: function (data, message, xhr)

is called and I can easilly access the String OK_MESSAGE by accessing data.

The problem es that POST method returns HttpStatus.BAD_REQUEST, AJAX

error: function (xhr, status, errMsg)

is called but I cannot access the String ERROR_MESSAGEsent by the Server, which I need to show the user.

Any suggestions?

SERVER SIDE: Spring Framework

I have a Spring Controller that has a method that returns the type ResponseEntity<String>.

For pletely good requests I return the following:

return new ResponseEntity<>(OK_MESSAGE, new HttpHeaders(), HttpStatus.OK);

But if there's any problem during the execution or Exception catching, I return:

return new ResponseEntity<>(ERROR_MESSAGE, new HttpHeaders(), HttpStatus.BAD_REQUEST);

Where ERROR_MESSAGE contains a customized String for each type of Exception catched.

CLIENT SIDE: AJAX call

When that POST method is called and returns HttpStatus.OK, AJAX

success: function (data, message, xhr)

is called and I can easilly access the String OK_MESSAGE by accessing data.

The problem es that POST method returns HttpStatus.BAD_REQUEST, AJAX

error: function (xhr, status, errMsg)

is called but I cannot access the String ERROR_MESSAGEsent by the Server, which I need to show the user.

Any suggestions?

Share Improve this question asked Jan 5, 2015 at 17:01 charliebrowniecharliebrownie 6,14711 gold badges39 silver badges56 bronze badges 3
  • Hi @charliebrownie can you please post the code of both ajax and controller? – liorsolomon Commented Jan 6, 2015 at 22:47
  • Hi! My Spring Controller method returns exactly that, a ResponseEntity<String>, where the String is that ERROR_MESSAGE variable. The only difference in the AJAX call is that when I get an OK response I can access that String in that data variable. Is there a way to access it from the error function? – charliebrownie Commented Jan 6, 2015 at 23:28
  • If you need the code I can do an edit or something, but I am wondering if there's a way to access that String I'm passing inside the ResponseEntity from the error function that I am missing... as there is a way from the AJAX success function. – charliebrownie Commented Jan 6, 2015 at 23:30
Add a ment  | 

1 Answer 1

Reset to default 7

On the controller I return the ResponseEntity in the following way:

return new ResponseEntity<>("Enter the full url", new HttpHeaders(), HttpStatus.BAD_REQUEST);

In the JS I would check the response error string in the following way:

$.ajax({
            url: 'http://localhost:8080/link',
            data: 'url=/www.stackoverflow.om',
            type: 'GET',

            contentType: 'application/json; charset=utf-8',
            success: function (data, textStatus, xhr) {
                alert(xhr.status);
            },
            error: function (data, textStatus, xhr) {
                alert(data.responseText);
            }
        })

本文标签: javascriptSpring ResponseEntity amp AJAX error function can39t access response body contentStack Overflow