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_MESSAGE
sent 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_MESSAGE
sent 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 thatERROR_MESSAGE
variable. The only difference in the AJAX call is that when I get an OK response I can access that String in thatdata
variable. Is there a way to access it from theerror
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 theerror
function that I am missing... as there is a way from the AJAXsuccess
function. – charliebrownie Commented Jan 6, 2015 at 23:30
1 Answer
Reset to default 7On 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);
}
})
版权声明:本文标题:javascript - Spring ResponseEntity & AJAX error function: can't access response body content - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743843341a2548679.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论