我在JSP中使用ajax post,将json数据发送到servlet java类。在servlet控制器类中,我使用getparameter来获取从调用JSP发送的数据。
到目前为止,一切正常。然后我从这个servlet类中启动数据处理,我需要制定一个数据响应以发送回调用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')
});
我的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?
非常感谢
答案 0 :(得分: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);
},
答案 1 :(得分:0)
如果您知道正在进入的数据的结构(您应该!),创建一个可以将帖子数据序列化的对象(我假设myData是json?...如果没有,它应该是! )由servlet。 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
}
这样做可以在前端收到响应时极大地帮助您,但这取决于您的需求。