赞
踩
vue 设置Axios允许携带cookie
import axios from 'axios'
Vue.prototype.axios =axios
axios.defaults.withCredentials = true
Django 设置settings允许携带cookie
CORS_ALLOW_CREDENTIALS = True
cookie的存:
response = HttpResponse('登陆成功') #改成重定向等都可以
response.set_cookie('username',username)
#response.set_signed_cookie('username',username,salt='asdasd') #带签名的
cookie(加盐)
response.set_cookie('password',password)
return response
cookie的取:
username = request.COOKIES.get('username')
#username = request.get_signed_cookie('username',salt='asdasd') #获取带签名的cookie(盐要相同,不然拿不到)
password = request.COOKIES.get('password')
cookie的删除:
response = HttpResponse('清除成功') #改成重定向等都可以
response.delete_cookie('username')
response.delete_cookie('password')
return response
PS:不设置过期时间的话,默认关闭浏览器就失效
session的存:
request.session['username'] = username
#上面这一句代码完成了以下事情:
#1、生成随机字符串
#2、将随机字符串写到用户浏览器cookie
#3、将随机字符串保存到服务器session
#4、在服务器随机字符串对应的字典中设置相关内容
request.session['password'] = password
return HttpResponse('登陆成功')
session的取:
username = request.session.get('username')
#username = request.session['username'] #两种方式都可以取,但是这种如果没取到会报错
password = request.session.get('password')
session的删除:
request.session.flush() #删除所有session
#del request.session['username'] #删除key为username的session
return HttpResponse('清除成功')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。