我想实现购物车跳转订单时候把商品ID转到订单页面在通过ID访问cookie里面的 商品商量可是 访问cookie的时候为null
Request.Cookies["shoppingCart"]这个等于空了 在购车的时候还有呢 到了订单就空了
HttpCookie cookie = new HttpCookie("shoppingCart");
在购物车里面设置了HttpCookie 为什么啊急急急 求各位大神 帮帮忙啊
HttpCookie cookie = new HttpCookie("ASP.NET_SessionId", "value");
cookie.Domain = "domain";
cookie.Path = "/";
Response.Cookies.Add(cookie);
页面转向前要设置一下
关键是我是用js表单提交跳转的
@啰嗦: 那你就用ajax跳转,不知道是不是下面的代码
前台:
1 $.ajax({ 2 type: 'POST', 3 url: 'testCookie1', 4 async: false, 5 data: { }, 6 success: function (result) { 7 $.ajax({ 8 type: 'POST', 9 url: 'testCookie2', 10 async: false, 11 data: {}, 12 success: function (result) { 13 14 15 } 16 }); 17 18 19 } 20 });
后台:
1 public void testCookie1() 2 { 3 HttpCookie cookie = new HttpCookie("shoppingCart", "value1"); 4 Response.Cookies.Add(cookie); 5 } 6 public void testCookie2() 7 { 8 object obj = Request.Cookies["shoppingCart"]; 9 }
购物车页面 :
js
//去结算
function GoSettlement(goods)
{
var data = "{'goods':'" + goods + "'}";
$.ajax({
type: "Post",
cache: false,
contentType: "application/json",
url: "/ShoppingCart/CookiePath?t=" + Math.random(),
data: data,
dataType: "text",
async: true,
success: function (m) {
if (m == "ok") {
$("#postGoods").val(goods);
$("#cartForm").submit();
} else {
xq_alert("结算失败", "warn");
}
},
error: function (e) {
xq_alert("链接服务器超时,请检查你的网络连接", "error");
}
});
}
后端:
[HttpPost]
public string AddShoppingCart(int goodId, string goodName, string mobilePic, string salePrice, int goodsNum_)
{
try
{
if (Request.Cookies["shoppingCart"] == null)
{
cookie = new HttpCookie("shoppingCart");
cookie.Values.Add("" + goodId + "", Server.UrlEncode("" + goodName + "," + mobilePic + "," + salePrice + "," + goodsNum_ + ""));
cookie.Path = "/";
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
}
else
{
cookie = Request.Cookies["shoppingCart"];
for (int i = 0; i < cookie.Values.Count; i++)
{
if (cookie.Values.AllKeys[i] == goodId.ToString())
{
string[] goods = Server.UrlDecode(cookie.Values[i]).Split(',');
cookie["" + goodId + ""] = Server.UrlEncode("" + goods[0] + "," + goods[1] + "," + goods[2] + "," + (Convert.ToInt32(goodsNum_) + Convert.ToInt32(goods[3])) + "");
break;
}
}
cookie.Values.Add("" + goodId + "", Server.UrlEncode("" + goodName + "," + mobilePic + "," + salePrice + "," + goodsNum_ + ""));
cookie.Path = "/";
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
}
return "ok";
}
catch (Exception)
{
return "no";
}
}
订单页面
后端:
public ActionResult SubmitOrder()
{
if (CurrentUserInfo == null)
{
return null;
}
string gids = Request.Form["postGoods"].ToString() ?? "";
string shopId = CurrentUserInfo.Id;
if (!string.IsNullOrEmpty(gids)) GoodsCookieById(gids);
ViewBag.Detail = shop.GetShopDetailBy(shopId);
ViewBag.Contact = shop.GetShopContactBy(shopId);
return View();
}
//根据ID获取购物车Cookie的数据
public HttpCookie GoodsCookieById(string gids)
{
if (Response.Cookies["shoppingCart"] == null)
{
string value = "";
string[] goods = gids.Split(',');
for (int i = 0; i < goods.Length; i++)
{
cookie = Response.Cookies["shoppingCart"];
for (int j = 0; j < cookie.Values.Count; j++)
{
if (cookie.Values.AllKeys[j] == goods[i])
{
value += cookie.Values[HttpUtility.UrlEncode(goods[i])];
break;
}
}
}
}
return null;
}
@twxstar: 我是用了ajax 可是还是不行 那个Response.Cookies["shoppingCart"] 到了订单页面就获取不到 在购物车还好好的到了页面跳转就失效了
@twxstar: 问题已解决谢谢了
域名呢
我没怎样过cookie 我用的是mvc 框架 是在 Controllers里面定义的HttpCookie 如果我想在订单里面使用购车里面的cookie (两个页面不在一个Controllers里面 ) 怎么获取啊 path怎么定义 求大神
@啰嗦: cookie的问题你还是在前端调试了.
你都设置cookie了还没怎么他.....
你在页面上看cookie是为什么没掉的.
提供几个排查思路:
1、在购物车页面设置cookie的地方,cookie的value是有长度限制的
2、保证购物车和订单的一级域名相同
3、看看你本地的浏览器安全性设置有无禁用cookie
4、其他场景,比如异步设置cookie也可能不成功
我没怎样过cookie 我用的是mvc 框架 是在 Controllers里面定义的HttpCookie 如果我想在订单里面使用购车里面的cookie (两个页面不在一个Controllers里面 ) 怎么获取啊 path怎么定义 求大神
cookie的domain, path都要设置。要不然默认就是当前域名和path
我没怎样过cookie 我用的是mvc 框架 是在 Controllers里面定义的HttpCookie 如果我想在订单里面使用购车里面的cookie (两个页面不在一个Controllers里面 ) 怎么获取啊 path怎么定义 求大神
@啰嗦: 那就直接设置path=/
@傲慢与偏剑:
我在创建的时候和跳转的时候都做了
cookie = new HttpCookie("shoppingCart");
cookie.Values.Add("" + goodId + "", Server.UrlEncode("" + goodName + "," + mobilePic + "," + salePrice + "," + goodsNum_ + ""));
cookie.Path = "/";
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
跳转
public string CookiePath()
{
if (Request.Cookies["shoppingCart"] != null)
{
try
{
cookie= Request.Cookies["shoppingCart"];
cookie.Path = "/";
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
return "ok";
}
catch (Exception)
{
return "no";
}
}
else
{
return "no";
}
}
首先要用Respone对象将cookie写到客户端,然后才可以用Request获取的。