js获取cookie乱码问题的解决办法……

来源:互联网 发布:php移动接口开发 编辑:程序博客网 时间:2024/05/15 06:04

本人在页面后台生成了cookie,名字为cok1,单位对其编码,然后再js中获取cokie,结果输出时是乱码……找了很多资料,又说在修改js的,本人都试了,似乎不管是,有说在后台解码的,想了想不合理,反过来试了下在后台编码cookie,结果成功了!   Response.Cookies.Add(new HttpCookie("cokie", Server.UrlEncode(checkCode)));

前台的js文件

jQuery.cookie = function (name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

获取时直接用$.cookie("cokie")即可!

呵呵,如有错误,请大家多多指点,共同进步哈!谢谢……

物流大集网,找专线,找车源,找货源就上物流大集网。

原创粉丝点击