jquery.cookie.js使用

1、下载jquery.cookie.js

官网:http://plugins.jquery.com/cookie/

或

http://pan.baidu.com/s/1mgynA8g

2、使用方法

复制代码
$.cookie('the_cookie'); // 获得cookie
$.cookie('the_cookie', 'the_value'); // 设置cookie
$.cookie('the_cookie', 'the_value', { expires: 7 }); //设置带时间的cookie
$.cookie('the_cookie', '', { expires: -1 }); // 删除
$.cookie('the_cookie', null); // 删除 cookie
$.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});//新建一个cookie 包括有效期 路径 域名等
复制代码
3、测试,HTML页中写一个cookie值,然后用ajax调用一个nginx请求,nginx里用LUA获取到这个cookie值再返回给HTML页最后alert出来。
HTML页

复制代码
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
<script>
    $.cookie('uid', 'dsideal');
</script>
</head>
<body>
    <p>
        <input type="button" value="test" onclick="test();" />
    </p>
</body>
<script>
    function test() {
        $.ajax({
            type : "GET",
            async : false,
            url : "http://10.10.3.205/getCookie",
            success : function(res) {
                alert(res);
            }
        });
    }
</script>
</html>
复制代码
nginx

location /getCookie{
            content_by_lua '
                ngx.header.content_type = "text/plain;charset=utf-8"
                ngx.say(ngx.var.cookie_uid)
            ';
        }
原文地址:https://www.cnblogs.com/archoncap/p/5393471.html