新手请教django2.2.3设置cookie返回值为null

新手请教django2.2.3设置cookie返回值为null,第1张

set_cookie()方法没有返回值。调用Response.set_cookie()即可给response对象设置cookie。

res = HttpRespnse('OK')

res.set_cookie('abc', 'abc')

return res

就是记录一些用户信息,比如你想记录用户的用户名你就可以写入cookie,以后要用时可以读取cookie来获取此信息,一般格式是:写入要写一个写入函数:如

function setCookie(c_name,value,expiredays)

{

var exdate=new Date()

exdate.setDate(exdate.getDate()+expiredays)

document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "

expires="+exdate.toGMTString())

}上面这个函数中的参数存有 cookie 的名称、值以及过期天数

之后,我们要创建另一个函数来检查是否已设置 cookie:

function getCookie(c_name)

{

if (document.cookie.length>0)

{

c_start=document.cookie.indexOf(c_name + "=")

if (c_start!=-1)

{

c_start=c_start + c_name.length+1

c_end=document.cookie.indexOf("",c_start)

if (c_end==-1) c_end=document.cookie.length

return unescape(document.cookie.substring(c_start,c_end))

}

}

return ""

}上面的函数首先会检查 document.cookie 对象中是否存有 cookie。假如 document.cookie 对象存有某些 cookie,那么会继续检查我们指定的 cookie 是否已储存。如果找到了我们要的 cookie,就返回值,否则返回空字符串。

最后,我们要创建一个函数,这个函数的作用是:如果 cookie 已设置,则显示欢迎词,否则显示提示框来要求用户输入名字。

function checkCookie()

{

username=getCookie('username')

if (username!=null &&username!="")

{alert('Welcome again '+username+'!')}

else

{

username=prompt('Please enter your name:',"")

if (username!=null &&username!="")

{

setCookie('username',username,365)

}

}

}

这是所有的代码:

<html>

<head>

<script type="text/javascript">

function getCookie(c_name)

{

if (document.cookie.length>0)

{

c_start=document.cookie.indexOf(c_name + "=")

if (c_start!=-1)

{

c_start=c_start + c_name.length+1

c_end=document.cookie.indexOf("",c_start)

if (c_end==-1) c_end=document.cookie.length

return unescape(document.cookie.substring(c_start,c_end))

}

}

return ""

}

function setCookie(c_name,value,expiredays)

{

var exdate=new Date()

exdate.setDate(exdate.getDate()+expiredays)

document.cookie=c_name+ "=" +escape(value)+

((expiredays==null) ? "" : "expires="+exdate.toGMTString())

}

function checkCookie()

{

username=getCookie('username')

if (username!=null &&username!="")

{alert('Welcome again '+username+'!')}

else

{

username=prompt('Please enter your name:',"")

if (username!=null &&username!="")

{

setCookie('username',username,365)

}

}

}

</script>

</head>

<body onLoad="checkCookie()">

</body>

</html>

我也碰到这个问题了,也很疑惑,同问。我的代码(JSP):

写cookie的部分:

Cookie cookie = new Cookie("write", "cookie_write")

cookie.setComment("a cookie test")

cookie.setVersion(100)

cookie.setMaxAge(60 * 60 * 1000)

String host=request.getHeader("host")

if(host.indexOf(":")>0){

host=host.split(":")[0]

}

cookie.setDomain(host)

cookie.setPath("/")//这行去掉和留下都试过,都一样

response.addCookie(cookie)

在最后这儿留断点,发现包括MaxAge之类都是对的。

读的部分:

Cookie cookies[] = request.getCookies()

if (cookies != null) {

out.println(" cookie:" + cookies.length)

out.println("<br>" + "<br>")

for (int i = 0i <cookies.lengthi++) {

out.println("getName=" + cookies[i].getName() + "<br>")

out.println("getValue=" + cookies[i].getValue() + "<br>")

out.println("getComment=" + cookies[i].getComment() + "<br>")

out.println("getDomain=" + cookies[i].getDomain() + "<br>")

out.println("getPath=" + cookies[i].getPath() + "<br>")

out.println("getPath=" + cookies[i].getPath() + "<br>")

out.println("getMaxAge=" + cookies[i].getMaxAge() + "<br>")

out.println("getVersion=" + cookies[i].getVersion() + "<br>")

out.println("getSecure=" + cookies[i].getSecure() + "<br>")

out.println("<br>" + "<br>")

}

}

读的这儿就不对了,都是null或者-1(MaxAge),但是能关了ie再开都能读到表示没过期(如果前面setMaxAge设置为-1,关了就没了)。

我也很疑惑,不知道有没有人一起研究一下。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/7999417.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存