cha*_*nie 6 javascript ajax jquery spring
服务器端:Spring框架
我有一个Spring Controller,它有一个返回类型的方法ResponseEntity<String>.
对于完全好的请求,我返回以下内容:
return new ResponseEntity<>(OK_MESSAGE, new HttpHeaders(), HttpStatus.OK);
Run Code Online (Sandbox Code Playgroud)
但是如果在执行或异常捕获期间出现任何问题,我会返回:
return new ResponseEntity<>(ERROR_MESSAGE, new HttpHeaders(), HttpStatus.BAD_REQUEST);
Run Code Online (Sandbox Code Playgroud)
其中ERROR_MESSAGE包含每种类型的Exception catched的自定义String.
客户端:AJAX调用
当调用该POST方法并返回HttpStatus.OK,AJAX时
success: function (data, message, xhr)
Run Code Online (Sandbox Code Playgroud)
被调用,我可以OK_MESSAGE通过访问轻松访问String data.
该问题涉及的是POST方法返回HttpStatus.BAD_REQUEST,AJAX
error: function (xhr, status, errMsg)
Run Code Online (Sandbox Code Playgroud)
被调用,但我无法访问ERROR_MESSAGE服务器发送的字符串,我需要向用户显示.
有什么建议?
在控制器上,我按以下方式返回ResponseEntity:
return new ResponseEntity<>("Enter the full url", new HttpHeaders(), HttpStatus.BAD_REQUEST);
Run Code Online (Sandbox Code Playgroud)
在JS中,我将以下列方式检查响应错误字符串:
$.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);
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8031 次 |
| 最近记录: |