如何通过在前端使用react adn adn django后端存储cookie
此后端的代码,我想在致电URL商店cookie
class StoreCookie(APIView):
def get(self, request):
if 'cookie' in request.COOKIES:
value = request.COOKIES['cookie']
print('store')
response = Response()
return response
else:
response = Response()
print('not store')
response.set_cookie('cookie', 'MY COOKIE VALUE', httponly=False)
return Response({"error": 'This Item Not Available For Now'}, status=status.HTTP_400_BAD_REQUEST)
之后 axios.get(`url`,{withcredentials:true})
this code for backend and I want after call URL store cookie but it still give me not sotre
class StoreCookie(APIView):
def get(self, request):
if 'cookie' in request.COOKIES:
value = request.COOKIES['cookie']
print('store')
response = Response()
return response
else:
response = Response()
print('not store')
response.set_cookie('cookie', 'MY COOKIE VALUE', httponly=False)
return Response({"error": 'This Item Not Available For Now'}, status=status.HTTP_400_BAD_REQUEST)
this code for frontendaxios.get(`url`,{withCredentials: true})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将在第二次工作。因为它第一次设置了cookie(因此它打印出“不存储”并设置cookie)。
当您第二次运行它时,cookie就在那里,因此它将正确打印cookie
I'll work for the 2nd time. Because the first time it set the cookies (so it prints 'not store' and set the cookie).
When you run it for the 2nd time, then the cookie is there, so it will print read the cookie correctly