Javaの道 Javaに関する
 ニュースJava基本Servlet・JSPオープンソースFAQ掲示板
Javaの道 >  掲示板 >  掲示板(jQuery.postのステータスを変える方法)
閲覧数:598
掲示板(jQuery.postのステータスを変える方法)
名前
匿名
題名 jQuery.postのステータスを変える方法
質問内容

質問を評価する
(0ポイント)
下記のようなコードでレスポンスのjspによって結果のHTMLを埋め込むタグを変えたいと思っています。

正常に処理できた場合は<div id="dataList">に結果のHTMLを埋め込む。
セッション切れやException発生時には<body>タグに結果のHTMLを埋め込む。
というように処理をしたいと考えています。

正常か異常かの判断にsearchFuncのmyStatusを使用しようとしていますが、常に'success'が帰ってきてしまいます。

処理を分けるよい方法はありませんか?

jspの内容
<script type="text/javascript">
<!--
$('#searchForm').change(function(e){
    $.post("/searchServlet", $('#searchForm').serialize(), searchFunc);
});
function searchFunc(myData, myStatus){
    if(myStatus == 'success') {
        $("#dataList").html(myData);
    } else {
        $("body").html(myData);
    }
};
-->
</script>
<body>
    <form id="searchForm" action="/searchServlet" method="post">
        検索条件いろいろ
    </form>
    <div id="dataList">
        検索結果がここに表示されます。
    </div>
</body>


Servletの内容
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        // ログインチェック
        if( initLoginCheck(request, response) == false ) {
            /* まだ認証されていない */
            RequestDispatcher dispatcher = request.getRequestDispatcher("/login.jsp");
            dispatcher.forward(request, response);
            return;
        }
        
        // データ検索
        List dataList = getDataList(request, response);
        request.setAttribute("dataList", dataList);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/dataListAjax.jsp");
        dispatcher.forward(request, response);
    } catch (Exception e) {
        String strMessage = e.toString();
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        strMessage += "\n" + sw.toString();
        request.setAttribute("ErrorStr", strMessage);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/errorPage.jsp");
        dispatcher.forward(request, response);
    }
}
質問日時 2013-09-02 10:11:30
名前
匿名
回答内容

回答を評価する
(0ポイント)
単にHttpステータスを見てるんじゃないのか?
jQueryのソースを覗いてみればいい。
回答日時 2013-09-02 20:43:27
名前
匿名
回答内容

回答を評価する
(0ポイント)
・q・?
サーブレットが401を返すつくりじゃないからね。
回答日時 2013-09-03 13:20:25
回答内容を入力し、「確認」ボタンをクリックしてください。
ログインしていません。ログインしなくても回答はできますが、ログインすると、質問・回答の管理、更新があった場合のメールでの通知を受けることができます。 ユーザIDをお持ちでない方は「ID登録」からユーザIDの登録を行ってください。
氏名 匿名
回答内容
 



このページのトップへ
 ニュースJava基本Servlet・JSPオープンソースFAQ掲示板
Javaの道_CopyrightJavaの道