AJAX Post-在JSP中执行,需要在AJAX Post Success函数中返回基于java的变量

AJAX Post-在JSP中执行,需要在AJAX Post Success函数中返回基于java的变量,java,ajax,jsp,spring-mvc,Java,Ajax,Jsp,Spring Mvc,我在JSP中使用ajax post,将json数据发送到servlet java类。在servlet控制器类中,我使用getparameter获取从调用JSP发送的数据 到目前为止,这一切都很好。然后,我从这个servlet类初始化数据的处理,并且我需要制定一个数据响应以发送回调用JSP 有没有一种方法可以将数据保存在servelt类的变量中,并作为success函数的一部分(在我的AJAX帖子中)访问这些数据 我的AJAX帖子代码: $.ajax({ type:

我在JSP中使用ajax post,将json数据发送到servlet java类。在servlet控制器类中,我使用getparameter获取从调用JSP发送的数据

到目前为止,这一切都很好。然后,我从这个servlet类初始化数据的处理,并且我需要制定一个数据响应以发送回调用JSP

有没有一种方法可以将数据保存在servelt类的变量中,并作为success函数的一部分(在我的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')                   
    }); 
我的Servlet控制器代码:

    @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函数的一部分,如何将这两个变量(responseText和responseCode)返回到调用JSP

也非常感谢

成功将返回响应

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);
    }, 

如果您知道即将传入的数据的结构(您应该!),则创建一个对象,该对象可以由servlet将post数据序列化到(我假设myData是json?…如果不是,它应该是!)。spring框架提供@RequestBody注释来将传入的json反序列化到对象。当servlet需要响应时,执行@Jigar建议的操作:将响应包装在对象中。spring框架提供@ResponseBody注释来序列化对json的响应。它可能看起来像这样:

你的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
}

在前端接收响应时,这样做非常有助于您,但这取决于您需要什么。

要使用JavaScript访问的任何内容都必须是发送到浏览器的响应的一部分。如何格式化取决于您,尽管JSON似乎是最好的选择。