//商品列表
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;
}