私はAngularJS + Spring4を使ってWebアプリケーションを開発しようとしています。私は何をしたいかエラーメッセージを読み込めませんでした:AngularJS - Spring 4
:
のhttp 1.If要求が(に表示されますJSON
例外の 2.Inケースとして応答データを送信する必要がある、カスタムエラーメッセージを送信する必要がある、成功です警告ボックスでユーザー)
春コントローラクラス:
@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コントローラ:
$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);
})
問題: エラーメッセージは読み取れませんが、成功データを読み取ることはできません。
私はコンソールにエラーを印刷このHTMLタグを取得しています:
<html><head><title>Error</title></head><body>Invalid input.</body></html>
AngularJSでこのエラーメッセージを解析する方法は?これは春からエラーメッセージを送信する正しい方法ですか?
同じJSON "responseJSONStr"でもエラーメッセージを送信すると、AngularJSの成功ハンドラで処理されます。この場合、レスポンスは成功と見なされます。
ガイダンスは非常に役立ちます。前もってありがとう:
成功パス行い、このJSONUtilities.toJson(responseJSON)以下のように「偽」としてisSuccessfulとしてresponseJSONに設定することができます。 - > JSONに何があなたのために何をするのですか? response.sendError(e.getErrorCode()、e.getErrorMessage());すなわち、あるシナリオはあなたの応答をJsonに変換しますが、他のシナリオはそうでないようです。つまり、 – 7663233
1.Jsonの文字列を返します。 2.私はUIにエラーメッセージを送って、それをユーザーに見せたいと思っています。私が同じ "responseJSONStr"でそのエラーメッセージを送信するとAngularJsの成功ブロックに来るので、私はそれを書いた.. – Ragul
@ 7663233 "ResponseJSONStr"からhttpステータスを読み取るようにAngularJSに指示する方法はありますか?サーバーから送信しているJSON? – Ragul