首页 新闻 会员 周边 捐助

我设置的cookie跳转页面后就访问不到了急急急 求各位大神 帮帮忙啊

0
悬赏园豆:20 [已解决问题] 解决于 2015-06-30 14:02

我想实现购物车跳转订单时候把商品ID转到订单页面在通过ID访问cookie里面的 商品商量可是 访问cookie的时候为null

Request.Cookies["shoppingCart"]这个等于空了 在购车的时候还有呢 到了订单就空了 

 HttpCookie cookie = new HttpCookie("shoppingCart");

 

在购物车里面设置了HttpCookie  为什么啊急急急  求各位大神 帮帮忙啊

啰嗦的主页 啰嗦 | 初学一级 | 园豆:6
提问于:2015-06-26 15:56
< >
分享
最佳答案
0

HttpCookie cookie = new HttpCookie("ASP.NET_SessionId", "value");
cookie.Domain = "domain";
cookie.Path = "/";
Response.Cookies.Add(cookie);

页面转向前要设置一下

收获园豆:8
twxstar | 菜鸟二级 |园豆:234 | 2015-06-27 10:21

关键是我是用js表单提交跳转的  

啰嗦 | 园豆:6 (初学一级) | 2015-06-27 15:46

@啰嗦: 那你就用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         }
twxstar | 园豆:234 (菜鸟二级) | 2015-06-27 16:16

 购物车页面 :

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;
}

啰嗦 | 园豆:6 (初学一级) | 2015-06-27 16:43

@twxstar: 我是用了ajax  可是还是不行  那个Response.Cookies["shoppingCart"] 到了订单页面就获取不到 在购物车还好好的到了页面跳转就失效了 

啰嗦 | 园豆:6 (初学一级) | 2015-06-27 16:45

@twxstar: 问题已解决谢谢了

啰嗦 | 园豆:6 (初学一级) | 2015-06-30 14:01
其他回答(4)
0

域名呢

吴瑞祥 | 园豆:29449 (高人七级) | 2015-06-26 15:59

我没怎样过cookie  我用的是mvc 框架 是在 Controllers里面定义的HttpCookie 如果我想在订单里面使用购车里面的cookie  (两个页面不在一个Controllers里面 ) 怎么获取啊 path怎么定义 求大神

支持(0) 反对(0) 啰嗦 | 园豆:6 (初学一级) | 2015-06-26 17:37

@啰嗦: cookie的问题你还是在前端调试了.

你都设置cookie了还没怎么他.....

你在页面上看cookie是为什么没掉的.

支持(0) 反对(0) 吴瑞祥 | 园豆:29449 (高人七级) | 2015-06-26 17:50
0

提供几个排查思路:

1、在购物车页面设置cookie的地方,cookie的value是有长度限制的

2、保证购物车和订单的一级域名相同

3、看看你本地的浏览器安全性设置有无禁用cookie

4、其他场景,比如异步设置cookie也可能不成功

收获园豆:6
JeffWong | 园豆:2328 (老鸟四级) | 2015-06-26 16:12

我没怎样过cookie  我用的是mvc 框架 是在 Controllers里面定义的HttpCookie 如果我想在订单里面使用购车里面的cookie  (两个页面不在一个Controllers里面 ) 怎么获取啊 path怎么定义 求大神

支持(0) 反对(0) 啰嗦 | 园豆:6 (初学一级) | 2015-06-26 17:37
0

cookie的domain, path都要设置。要不然默认就是当前域名和path

收获园豆:3
傲慢与偏剑 | 园豆:381 (菜鸟二级) | 2015-06-26 16:52

我没怎样过cookie  我用的是mvc 框架 是在 Controllers里面定义的HttpCookie 如果我想在订单里面使用购车里面的cookie  (两个页面不在一个Controllers里面 ) 怎么获取啊 path怎么定义 求大神

支持(0) 反对(0) 啰嗦 | 园豆:6 (初学一级) | 2015-06-26 17:37

@啰嗦:  那就直接设置path=/

支持(0) 反对(0) 傲慢与偏剑 | 园豆:381 (菜鸟二级) | 2015-06-27 06:53

@傲慢与偏剑: 

我在创建的时候和跳转的时候都做了

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";
}
}

支持(0) 反对(0) 啰嗦 | 园豆:6 (初学一级) | 2015-06-27 16:47
0

首先要用Respone对象将cookie写到客户端,然后才可以用Request获取的。

收获园豆:3
幻天芒 | 园豆:37205 (高人七级) | 2015-06-27 09:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册