我用ajax+cookies做购物车,修改后 cookies的值不变,这是为什么呀?,代价如下:
/// <summary>
/// 将要购买的商品信息写入Cookies
/// </summary>
public void WriteCookies(String ProductID, String Amount)
{
HttpContext.Current.Response.Cookies["ShoppingCart"]["ProductID"] = ProductID;
HttpContext.Current.Response.Cookies["ShoppingCart"]["Amount"] = Amount;
HttpContext.Current.Response.Cookies["ShoppingCart"].Expires = DateTime.Now.AddDays(60);
//GetDataFromCookies();
}
修改后
HttpContext.Current.Response.Cookies["ShoppingCart"]["ProductID"] = productId;
HttpContext.Current.Response.Cookies["ShoppingCart"]["Amount"] = amount;
HttpContext.Current.Response.Cookies["ShoppingCart"].Expires = DateTime.Now.AddDays(60);
修改后的amount数量,变了,,修改完之后,在取值
IList<Product> iList = new List<Product>();
if (HttpContext.Current.Request.Cookies["ShoppingCart"] != null)
{
string[] split = HttpContext.Current.Request.Cookies["ShoppingCart"]["ProductID"].ToString().Trim().Split(new Char[] { ',' });
string[] splitAmount = HttpContext.Current.Request.Cookies["ShoppingCart"]["Amount"].ToString().Trim().Split(new Char[] { ',' });
for (int i = 0, j = 0; i < split.Length && j < splitAmount.Length; i++, j++)
{
后面我就不贴了, 这里取到的值是 splitAmount 是修改之前的值,上面,重新复制后,没取到,这是ajax修改操作,不刷新,就这样,难道不刷新,cookies的值不变吗?
大哥你的缓存
上次也试过这种写法,无法取到cookie值,后来修改为返回json,然后在callback中进行操作:
$.post(loginUrl,data,function(data){
if(data.Result=="False"){
$("#message").empty()
.append('系统登陆失败,请核对用户名和密码');
}else{
document.location.href="main.htm";
$.cookie('guid',data.GUID,{expires: 7});
$.cookie('name',data.Name,{expires: 7});
}
});