//写入临时cookie
var data = [];
var data ={${mallProduct.id}:{mallProductName:"${mallProduct.mallProductName}",productId:${mallProduct.id},unit:"台",quantity:bCount,price:${mallProduct.getPriceByType(identityType) },bindingCount:bCount}};
console.log(JSON.stringify(data));
JMCookie.setCookie("tmp",JSON.stringify(data));
//传入参数,fromCart = true
location.href="/reward/mallConfirm?direct=true&paymentType=${mallProduct.paymentType}&mallType=${mallType}";
cosole.log的结果:
{"274":{"mallProductName":"智能电视遥控插座(USB版)","productId":274,"unit":"台","quantity":10,"price":75,"bindingCount":10}}
apllication:cookies中tmp的value值,应该是进行了编码,以下是改正后的编码是正常的,USB版前后的中文括号都进行的编码。
%7B%22274%22%3A%7B%22mallProductName%22%3A%22%E6%99%BA%E8%83%BD%E7%94%B5%E8%A7%86%E9%81%A5%E6%8E%A7%E6%8F%92%E5%BA%A7%EF%BC%88USB%E7%89%88%EF%BC%89%22%2C%22productId%22....
改正前由于USB版前后括弧是英文版所以放入cookie中时编码是不一样的,括号还在,没有编码:
%7B%22274%22%3A%7B%22mallProductName%22%3A%22%E6%99%BA%E8%83%BD%E7%94%B5%E8%A7%86%E9%81%A5%E6%8E%A7%E6%8F%92%E5%BA%A7%EF%BC%88(USB%E7%89).........
传入后台时在后台获取cookie得到的结果是(及之后的内容都没有了。也就是cookiData的内容不全,导致json解析式报错
if(direct) cookie_key = "tmp";
String cookieData = this.getCookie(cookie_key);
log.info("testing:" + cookieData );
if(StringUtils.isEmpty(cookieData)) cookieData="{}";
JSONObject objJson= JSONObject.parseObject(URLDecoder.decode(cookieData,"utf-8"));
解决的方法:就是把英文的括号换成中文的
当然根本原因还是不清楚,为什么英文括号设置cookie时没有编码保存。
附前端设置cookie的方法:
/*Mustache {{#}}{{/}} 循环
{{#}}{{/}}这样的语法,除了上边的循环遍历之外,
它还有另外的一层意思就是判空,如果{{#}}中的值为null或false或undefine则其标记内的内容则不展现
有了判空的方法当然还有与之相反的方法{{^}},该方法表示的意思与{{#}}意思相反
{{&}},这样就可以防止转义。
*/
/*左侧分类*/
var JMCookie = (function(){
return {
setCookie : function (name, value,para) {
var domain = document.domain;
var path = "/";
var expires = new Date();
expires.setHours(expires.getHours()+1);
if(para){
if(para["expires"]){
expires = para["expires"];
}
if(para["path"]){
path = para["path"];
}
if(para["domain"]){
domain = para["domain"];
}
}
document.cookie = name + "=" + encodeURIComponent(value) + "; expires=" + expires.toGMTString() + ";path=" + path;
},
getCookie : function (name) {
var value = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if (value != null) {
return decodeURIComponent(value[2]);
} else {
return null;
}
},
removeCookie : function (name) {
var expires = new Date();
expires.setTime(expires.getTime() - 1000 * 60);
this.setCookie(name, "", expires);
}
};
})();