简体   繁体   English

无法读取错误消息:AngularJS-Spring 4

[英]Couldn't read error message : AngularJS - Spring 4

I'm trying to develop a Web application using AngularJS + Spring4. 我正在尝试使用AngularJS + Spring4开发Web应用程序。

What i want to do: 我想做的事:

1.If http request is success, needs to send response data as JSON 1.如果http请求成功,则需要以JSON格式发送响应数据
2.In case of an exception, needs to send custom error message (will be shown to the user in alert box) 2.如果发生异常,则需要发送自定义错误消息(将在警报框中显示给用户)

Spring controller class: 弹簧控制器类:

@RequestMapping(value = "/loadAllUsers", method = RequestMethod.POST)
@ResponseBody
public String loadAllUsers(@RequestBody String paramsJsonStr,ModelMap model,HttpServletRequest request, HttpServletResponse response) throws IOException {

String responseJSONStr = null;
ResponseJSON responseJSON = new ResponseJSON(); //Custom class for sending response data 

try {
    .....
    .....
    List<User> users = this.loadAllUsers();
    responseJSON.setIsSuccessful(true);
    responseJSON.setData(schemas);
    responseJSONStr = JSONUtilities.toJson(responseJSON);
}catch (CustomException e) {
    e.printStackTrace();
    response.sendError(e.getErrorCode(), e.getErrorMessage());
}

   return responseJSONStr;
}

AngularJs controller: AngularJs控制器:

$http.post("loadAllUsers",{})
.success(function(data){
    console.log('success handler');
    console.log(data);
})
.error(function(error) {
    console.log('error handler');
    console.log(error);
    console.log(error.status);
    console.log(error.data);
    console.log(error.statusText );
    console.log(error.headers);
    console.log(error.config);
})

Problem : Not able to read error message , but able to read the success data. 问题:无法读取错误消息,但是能够读取成功数据。

when i print the error in console am getting this HTML tags: 当我在控制台中打印错误时,得到此HTML标签:

<html><head><title>Error</title></head><body>Invalid input.</body></html>

How to parse this error message in AngularJS ? 如何在AngularJS中解析此错误消息? Is this the correct way to send error message from spring ?? 这是从Spring发送错误消息的正确方法吗?

If i send the error message also in same JSON "responseJSONStr", it'll be processes in success handler of the AngularJS as response will be considered as success in this case. 如果我也以相同的JSON“ responseJSONStr”发送错误消息,它将是AngularJS成功处理程序中的进程,因为在这种情况下,响应将被视为成功。

Any guidance will be very helpful. 任何指导都将非常有帮助。 Thanks in advance :) 提前致谢 :)

In case anyone in future tried to do the same this may help.. To achieve this, instead of using "response.sendError" a failure status code can be set using "response.setStatus" and error message can be set in responseJSON with isSuccessful as "false" as below 如果将来有人尝试这样做,可能会有所帮助。.要实现此目的,可以使用“ response.setStatus”设置故障状态代码,而不是使用“ response.sendError”,并且可以使用isSuccessful在responseJSON中设置错误消息如下所示为“ false”

@RequestMapping(value = "/loadAllUsers", method = RequestMethod.POST)
@ResponseBody
public String loadAllUsers(@RequestBody String paramsJsonStr,ModelMap model,HttpServletRequest request, HttpServletResponse response) throws IOException {

String responseJSONStr = null;
ResponseJSON responseJSON = new ResponseJSON(); //Custom class for sending response data 

try {
     .....
     .....
     List<User> users = this.loadAllUsers();
     responseJSON.setIsSuccessful(true);
     responseJSON.setData(schemas);
}catch (CustomException e) {
     e.printStackTrace();
     response.setStatus(e.getErrorCode()); //http failure status code as per respective error
     responseJSON.setIsSuccessful(false);
     responseJSON.setMessage(e.getErrorMessage());
}

 responseJSONStr = JSONUtilities.toJson(responseJSON);
 return responseJSONStr;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “无法读取依赖项”错误与npm - “Couldn't read dependencies” error with npm 无法读取AngularJS中未定义错误消息的属性&#39;then&#39; - Cannot read property 'then' of undefined error message in AngularJS AngularJS $ state无法返回 - Angularjs $state couldn't navigate back AngularJS templateUrl在ngInclude中找不到ngTemplate - Angularjs templateUrl couldn't find ngTemplate in ngInclude 错误:exportArchive:无法读取数据,因为它的格式不正确 - error: exportArchive: The data couldn’t be read because it isn’t in the correct format phonegap代码:2消息:操作无法完成。 (kCLErrorDomain错误0。) - phonegap code: 2 message: The operation couldn’t be completed. (kCLErrorDomain error 0.) AngularJS中的ngMessages验证无法清除CSS中的错误消息 - ngMessages validation in AngularJS doesn't clean the error message in CSS 我收到此错误“无法读取 null 的属性 &#39;getBBox&#39;”,我不明白为什么? - I get this error " Cannot read property 'getBBox' of null " and I couldn't understand why? npm install:install无法读取依赖项 - npm install: install Couldn't read dependencies 无法将span元素追加到Angularjs / Jquery中的数组对象 - Couldn't append span element to array object in Angularjs/Jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM