2017-03-23 7 views
0

私は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の成功ハンドラで処理されます。この場合、レスポンスは成功と見なされます。

ガイダンスは非常に役立ちます。前もってありがとう:

+0

成功パス行い、このJSONUtilities.toJson(responseJSON)以下のように「偽」としてisSuccessfulとしてresponseJSONに設定することができます。 - > JSONに何があなたのために何をするのですか? response.sendError(e.getErrorCode()、e.getErrorMessage());すなわち、あるシナリオはあなたの応答をJsonに変換しますが、他のシナリオはそうでないようです。つまり、 – 7663233

+0

1.Jsonの文字列を返します。 2.私はUIにエラーメッセージを送って、それをユーザーに見せたいと思っています。私が同じ "responseJSONStr"でそのエラーメッセージを送信するとAngularJsの成功ブロックに来るので、私はそれを書いた.. – Ragul

+0

@ 7663233 "ResponseJSONStr"からhttpステータスを読み取るようにAngularJSに指示する方法はありますか?サーバーから送信しているJSON? – Ragul

答えて

0

これを実現するには、「response.sendError」を使用する代わりに、「response.setStatus」を使用してエラーステータスコードを設定し、エラーメッセージが

@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; 
} 
関連する問題