ajax_典型应用_添加商品
1、设计思路
2、关键代码
<a href=”addToCart?id=oracle&price=200”>加入购物车 ShoppingCart.java Map<A,B> items = new HashMap<A,B>(); items.containsKey(bookName) $.getJSON(url,args,function(data){ }) void doGet(…){ doPost(….)
}
vid doPost(){
String bookName = request.getParameter(“id”);
int price =
HttpSession session = request.getSession();
ShoppingCart sc = (ShoppingCart) session.getAttribute(“sc”);
if(sc = null){
sc = new Shopping Cart();
session.setAttribute(“sc”,sc);
}
sc.addToCart(bookName.price);
StringBuilder result = new StringBuilder();
result.append(“{”)
.append(“’bookname’:’”+bookName+”’”)
.
setContentttype(“text/javascript”);
setChar
}
preview 产看服务器响应的数据结果
从服务端响应的json对象必须是双引号
不需要时隐藏页面显示
|
var isHasCart = “${sessionScope.sc == null}”;
alert(isHasCart);
if(isHashCart == “true”){
$(“#cartstatus”).hide();
}else{
$(“#cartstatus”).show();
}
时间: 2024-10-13 19:21:29