我想实现一个JS的购物车,就可能会用到Cookie,我就做纯Aajx效果, 具体如下:页面有一张表的数据, 我选中一行 点击放入购物车按钮  就保存一条信息  这条信息是放入Cookie中的,  然后点取回按钮 就是读取Cookie里面的内容数据  ; 望大神指点.

解决方案 »

  1.   

    既然都用ajax了,为什么不直接将数据保存到服务端?用户关闭了Cookie功能你就没办法了
      

  2.   


    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="获取Cookie.aspx.cs" Inherits="WebApplication1.获取Cookie" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript" src="Scripts/jquery-1.4.1-vsdoc.js"></script>
        <script type="text/javascript">
            $(function () {
                var ids = "1,2,3,4,5,"; var userID = 22;
                $("#Button1").click(function () {
                    $.ajax({
                        url: "Handgoods.ashx",
                        type: "post",
                        data: { id: ids, userID: userID, type: "add" },
                        success: function (msg) {
                            if (msg != "") {
                                $("#addGoods").html("<font color='red'>放入购物车成功</font>");
                            }
                        }
                    });
                });
                $("#Button2").click(function () {
                    $.ajax({
                        url: "Handgoods.ashx",
                        type: "post",
                        data: { userID: userID, type: "read" },
                        success: function (read) {
                            if (read != "") {
                                $("#getGoods").html("<font color='red'>"+read+"</font>");
                            }
                            else {
                                $("#getGoods").html("<font color='red'>读取失败</font>");
                            }
                        }
                    });
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input id="Button1" type="button" value="加入购物车" />
            <div id="addGoods" style="font-size: 12px;">
            </div>
            <input id="Button2" type="button" value="显示购物车" />
            <div id="getGoods">
            </div>
        </div>
        </form>
    </body>
    </html>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;namespace WebApplication1
    {
        /// <summary>
        /// Handgoods 的摘要说明
        /// </summary>
        public class Handgoods : IHttpHandler
        {        public void ProcessRequest(HttpContext context)
            {
                string goodsID = context.Request["id"];
                int userID = int.Parse(context.Request["userID"]);//int.Parse(context.Request["userID"])如果为空就会报错,而如果context.Request["userID"]为空,那么userID就为0
                if (context.Request["type"] == "add")
                {
                    try
                    {
                        string[] goodsIDList = goodsID.ToString().Split(new Char[] { ',' });
                        int goodsListCount = goodsIDList.Count();
                        string goodsNewID = null;
                        for (int i = 0; i < goodsListCount; i++)
                        {
                            goodsNewID += goodsIDList[i];
                        }
                        HttpCookie cookie = new HttpCookie(goodsID);//初使化并设置Cookie的名称
                        cookie.Expires = DateTime.Now.AddMinutes(10);
                        cookie.Values.Add("", goodsNewID);
                        context.Response.AppendCookie(cookie);
                        context.Response.Write("成功");
                    }
                    catch (Exception ex)
                    {
                        context.Response.Write(ex.ToString());
                    }
                }
                if (context.Request["type"] == "read")
                {
                    try
                    {
                        if (context.Request.Cookies[goodsID] != null)
                        {
                            string cookID = context.Request.Cookies[goodsID].Value;
                            context.Response.Write(cookID);
                        }
                        else
                        {
                            context.Response.Write("为空");
                        }
                    }
                    catch (Exception ex)
                    {
                        context.Response.Write(ex.ToString());
                    }
                }
            }        public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }