I'm new to ajax and like how it can do things asynchronously without having to reload the page. If I wanted to update my shopping cart total without having to actually hit the "update" button could it be done with the XMLHttpRequest object in this. Thanks for the help.
<%
ShoppingCart cart = (ShoppingCart)session.getAttribute("cart");
if(cart==null){
response.sendRedirect("manageCart.jsp");
}else{
//We have the ca开发者_如何学编程rt, let's update it
int itemCount = cart.getItemCount();
int quant = 0;
for(int i=itemCount-1; i>=0; i--){
try{
//User may have erased quantity or entered non-numeric value
quant = new Integer(request.getParameter("item" + i)).intValue();
}catch(NumberFormatException nfe){
quant = 1;
}
//Get the next Item
Item item = cart.getItem(i);
//Set its quantity
item.setQuantity(quant);
if(request.getParameter("remove" + i)!=null){
//Remove checkbox checked
cart.removeItem(i);
}
}
//Put the cart back in the session
session.setAttribute("cart",cart);
//go look at it!
response.sendRedirect("manageCart.jsp");
}
%>
Ajax only handles the part after the "update order" is issued. If you want to avoid having to hit the update button you will need to trigger the Ajax request in a different manner, perhaps with a set-interval function of some sor?
精彩评论