如何通过在前端使用react adn adn django后端存储cookie

发布于 2025-01-22 08:24:16 字数 616 浏览 2 评论 0原文

此后端的代码,我想在致电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 frontend
axios.get(`url`,{withCredentials: true})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷清清 2025-01-29 08:24:16

我将在第二次工作。因为它第一次设置了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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文