1

JSP内でajaxポストを使用して、jsonデータをサーブレットJavaクラスに送信しています。サーブレット コントローラ クラス内で getparameter を使用して、呼び出し元の JSP から送信されるデータを取得しました。

この時点まで、これはすべて正常に機能します。次に、このサーブレット クラスからのデータの処理を開始し、呼び出し元の JSP に送り返すデータ応答を作成する必要があります。

servelt クラス内の変数にデータを保持し、(AJAX 投稿内の) 成功関数の一部としてこのデータにアクセスする方法はありますか?

私のAJAXポストコード:

    $.ajax({ 
        type:        "POST", 
        url:           url, 
        dataType:  "text",  // [text, xml, json, script, text, html]
        data:       {postData : myData, sendURL : postUrl}, 
        success:    function(data, textStatus, jqXHR) { 
            alert('Success post to URL entered \n\n The data returned the following: ' + data);
        }, 
        error:function (xhr, ajaxOptions, thrownError){ 
            alert('Error xhr : ' + xhr.status); 
            alert('Error thrown error: ' + thrownError); 
        }
        //complete: alert('complete')                   
    }); 

私のサーブレットコントローラーコード:

    @RequestMapping("/postData")
    public String postData(Model model, HttpServletRequest request) throws Throwable{

        String postData = request.getParameter("postData");
        String sendURL= request.getParameter("sendURL");

        System.out.println(this.getClass() + " : postData   : " + postData);
        System.out.println(this.getClass() + " : gatewayURL : " + gatewayURL);

        /* Process data and formulate a response.... */

        String responseText = processedResponseText; // This processedResponseText was populated in the internal processing
        String responseCode = processedResponseCode; // This processedResponseCode was populated in the internal processing

        return "callingJSP";
    }

私の AJAX Post - Success 関数の一部として、これら 2 つの変数 (responseText と responseCode) を呼び出し元の JSP に戻すにはどうすればよいですか?

どうもありがとう

4

2 に答える 2

0

入ってくるデータの構造を知っている場合 (そうするべきです!)、投稿データをシリアル化できるオブジェクトを作成します (myData は json だと思いますか? ... そうでない場合は、そうあるべきです!)サーブレット。Spring フレームワークは、受信した json をオブジェクトに逆シリアル化する @RequestBody アノテーションを提供します。サーブレットが応答する必要がある場合は、@Jigar が推奨することを行います。応答をオブジェクトにラップします。Spring フレームワークは、json へのレスポンスをシリアル化する @ResponseBody アノテーションを提供します。次のようになります。

あなたのjs:

var myData = { postId: 1, comment: "this is great!" };
 $.ajax({ 
        type:        "POST", 
        url:           url, 
        dataType:  "text",  // [text, xml, json, script, text, html]
        data:       {postData : myData, sendURL : postUrl}, 
        success:    function(data, textStatus, jqXHR) {
            var jsonRepsonse = JSON.parse(data);
            alert('Success post to URL entered \n\n The data returned the following: ' + jsonRepsonse.responseText + ", " + jsonRepsonse.responseCode);
        }, 
        error:function (xhr, ajaxOptions, thrownError){ 
            alert('Error xhr : ' + xhr.status); 
            alert('Error thrown error: ' + thrownError); 
        }
        //complete: alert('complete')                   
    });

あなたの Java オブジェクト:

class Comment {
  private long postId;
  private String comment;
  // getters & setters
}

ラップされた応答オブジェクト:

class AjaxResponse{
 private String responseText;
 private String responseCode;
 //other stuff
}

コントローラーのハンドラー関数:

 @RequestMapping("/postData")
 public @ResponseBody postData(Model model, 
             @RequestBody Comment comment,
             HttpServletRequest request) throws Throwable{

    String sendURL= request.getParameter("sendURL");

    System.out.println(this.getClass() + " : comment : " + comment.toString());

    /* Process data and formulate a response.... */

    AjaxResponse ajaxResponse = new AjaxResponse(processedResponseText, processedResponseCode);

    return ajaxResponse;
 }

AjaxResponse には、レスポンスに関する詳細情報を提供するテキストではなく、別のオブジェクトが含まれていることが理想的です。たとえば、AjaxResponse オブジェクトを次のように変更できます。

class CommentResponse extends Comment {
   private long commentId;
   private Timestamp entryDateTime;
   // etc
}

class AjaxResponse{
 private CommentResponse commentResponse;
 private String responseCode;
 //other stuff
}

これを行うと、フロントエンドで応答を受信するときに非常に役立ちますが、必要なものによって異なります。

于 2012-07-10T18:50:46.420 に答える
0

また..

成功すると応答が返されます

success:    function(data, textStatus, jqXHR) { 
       alert('Success post to URL entered \n\n The data returned the following: ' + data);
    }, 

成功関数に XHR と textStatus は必要ありません。次のようにする必要があります。

success:    function(response) { 
       alert('Success post to URL entered \n\n The data returned the following: ' + response.responseText);
    }, 
于 2012-07-10T11:11:37.393 に答える