0

スプリング コントローラ:

response.sendError(500, ErrorMessage.DataBaseInsertFailed);

私の.jspページ

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

    }

e.status を実行すると、500 が返されます

e.resonseText を実行すると、テキスト全体が表示されますが、カスタム メッセージ 'Database failure due to ...' を表示したいのですが、アラート ステートメントが起動しません。私がalert(xhr)それをしても、どちらも発火しません。

上記はSOの this one からのものです

このドキュメントを見て、ドキュメントと一致するように署名を変更しました

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.
4

1 に答える 1

0

500 エラー応答を送信しているため、表示されている HTML はサーバーのエラー ページです。応答ステータスを設定し、次のように JSON を応答に書き込みます。

((HttpServletResponse)response).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().write(ErrorMessage.DataBaseInsertFailed));

次に、ajax 呼び出しで、エラー コールバックを使用してエラーを表示します。

error: function(xmlHttpRequest, textStatus, errorThrown) {
    alert(xmlHttpRequest.responseText);
}
于 2013-03-21T15:00:23.350 に答える