我正在使用jQuery / AJAX调用将控制权传递给servlet,并在成功将控制权发送到另一个servlet之后。不知道如何将第一个sevlet设置的JSON对象检索到第二个。这是我的伪代码。
orders.jsp
---------------
// display orders
// on click calls following ajax
$.ajax({
url : "processorder",
type : "POST",
dataType : "text",
data : formData,
success : function(data, textStatus, jqXHR) {
var successUrl = "checkout"; //upon success pass the control to checkout.java
window.location.href = successUrl;
return false;
},
error : function(jqXHR, textStatus, errorThrown) {
alert("Oops ! Error occurred !");
}
});// End of ajax
processorder.java (servlet)
-------------------------------
Processes data
Set some session variables
List<OrderDetails> newod = new ArrayList<OrderDetails>();
Gson gson = new Gson();
JsonObject jsonObject = new JsonObject();
JsonElement orderDetailElement = null;
//update orderDetailElement
orderDetailElement = gson.toJsonTree(newod);
jsonObject.add("OrderDetails", orderDetailElement);
request.setAttribute("OrderDetails", newod); //set the session with orderdetails
out.print(jsonObject.toString()); // write object to json
checkout.java (servlet)
----------------------------
**How can I access JSON object which I am sending from processorder?**
任何想法?
答案 0 :(得分:1)
您需要在第一次ajax调用的成功函数内进行ajax调用。
像
这样的东西$.ajax({
url : "processorder",
type : "POST",
dataType : "text",
data : formData,
success : function(response) {
$.ajax({
url : "checkout",
type : "POST",
dataType : "text",
data : response,
success : function(response) {
},
error : function(jqXHR, textStatus, errorThrown) {
alert("Error ");
}
});
},
error : function(jqXHR, textStatus, errorThrown) {
alert("Oops ! Error occurred !");
}
});