Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax 无法从responseText获取消息_Ajax_Spring Mvc - Fatal编程技术网

Ajax 无法从responseText获取消息

Ajax 无法从responseText获取消息,ajax,spring-mvc,Ajax,Spring Mvc,弹簧控制器: response.sendError(500, ErrorMessage.DataBaseInsertFailed); 我的.jsp页面 complete: function(e, xhr, settings){ var errorMessage = $.parseJSON(e.responseText); alert(errorMessage); } 如果我做e.status,我会得到500 如果我使用e.resonseText,我会

弹簧控制器:

response.sendError(500, ErrorMessage.DataBaseInsertFailed);
我的.jsp页面

    complete: function(e, xhr, settings){
    var errorMessage = $.parseJSON(e.responseText);
        alert(errorMessage);

    }
如果我做e.status,我会得到500

如果我使用e.resonseText,我会得到整个文本,但是我希望显示我的自定义消息“由于…导致数据库失败”,我的警报语句甚至不会触发。如果我发出
警报(xhr)
也不会触发

以上是从这一个在中国

查看此文件并更改我的签名以匹配文档

complete
Type: Function( jqXHR jqXHR, String textStatus )
A function to be called when the request finishes (after success and error callbacks are executed). The function gets passed two arguments: The jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object and a string categorizing the status of the request ("success", "notmodified", "error", "timeout", "abort", or "parsererror"). As of jQuery 1.5, the complete setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.

您看到的HTML是服务器的错误页面,因为您正在发送500个错误响应。设置响应状态并将JSON写入响应,如下所示:

((HttpServletResponse)response).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().write(ErrorMessage.DataBaseInsertFailed));
然后在ajax调用中,使用错误回调显示错误:

error: function(xmlHttpRequest, textStatus, errorThrown) {
    alert(xmlHttpRequest.responseText);
}

您的Spring控制器是否实际返回JSON格式的数据…?这很有趣,
alert(e.responseText)
为我提供了一个html格式的字符串,而隐藏在正文中的是我的自定义消息