zoukankan      html  css  js  c++  java
  • cookie的存储与取值

    //商品列表

    var id=@Session["Id"];
    function load() {
    $.ajax({
    url: "http://localhost:52975/api/Goods/GetGood/"+id,
    type: "post",
    dataType: "json",
    success:
    function (d) {
    $("#name").text(d.Name);
    $("#price").text("¥"+d.Price+"元");
    }
    })
    }
    load();

    if (getCookie("shopcar")==null) {
    setCookie("shopcar", "[]");
    }
    function add() {
    var x = {
    Name: $("#name").text(),
    Price:$("#price").text()
    };
    var liststr = getCookie('shopcar');
    var list = JSON.parse(liststr);
    list.push(x);
    setCookie("shopcar", JSON.stringify(list));
    location.href = '/Goods/ShopCar';
    }

    /**
    * cookie中存值
    * */
    function setCookie(name, value) {
    if (value) {
    var days = 1; //定义一天
    var exp = new Date();
    exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
    // 写入Cookie, toGMTString将时间转换成字符串
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString;
    }
    };
    /**
    * cookie中取值
    * */
    function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); //匹配字段
    if (arr = document.cookie.match(reg)) {
    return unescape(arr[2]);
    } else {
    return null;
    }
    };

    //购物车

    function load() {
    var liststr = getCookie("shopcar");
    var list = JSON.parse(liststr);
    $("#tb").empty();
    $(list).each(function () {
    $("#tb").append(
    '<tr>' +
    '<td>' + this.Name + '</td>' +
    '<td>' + this.Price + '</td>' +
    '</tr>'
    )
    })
    }
    load();

    [HttpPost]
    public int AddCart(int id)
    {
    var res = 0;
    HttpCookie cookie = new HttpCookie("商品页面-");
    cookie.Value = id.ToString();
    cookie.Expires = DateTime.Now.AddDays(1);
    Response.Cookies.Add(cookie);
    return res;
    }

  • 相关阅读:
    python2和3使用pip时的问题
    Python爬虫-爬取百度贴吧帖子
    Python爬虫-爬取糗事百科段子
    keras例子-matchnet
    win10-Anaconda2-Theano-cuda7.5-VS2013
    caffe-win10-cifar10另
    PHP——0126最初
    PHP——动态随机数
    PHP——投票
    PHP——内测:联系人管理
  • 原文地址:https://www.cnblogs.com/xiaowangtongxue123/p/13217224.html
Copyright © 2011-2022 走看看