<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: 廖珞君</title>
    <description>The latest articles on DEV Community by 廖珞君 (@luke_liao).</description>
    <link>https://dev.to/luke_liao</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2079106%2Fff7c4ef5-6cf4-4fc5-9f59-bedb24af7f22.png</url>
      <title>DEV Community: 廖珞君</title>
      <link>https://dev.to/luke_liao</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/luke_liao"/>
    <language>en</language>
    <item>
      <title>&lt;Issue&gt;React unable to successfully transfer cookies with backend</title>
      <dc:creator>廖珞君</dc:creator>
      <pubDate>Mon, 16 Sep 2024 07:42:59 +0000</pubDate>
      <link>https://dev.to/luke_liao/react-unable-to-successfully-transfer-cookies-with-backend-2acj</link>
      <guid>https://dev.to/luke_liao/react-unable-to-successfully-transfer-cookies-with-backend-2acj</guid>
      <description>&lt;p&gt;The following is my backend program:&lt;br&gt;
corsconfig:&lt;br&gt;
app.use(cors({&lt;br&gt;
    origin:'&lt;a href="http://localhost:5173" rel="noopener noreferrer"&gt;http://localhost:5173&lt;/a&gt;',&lt;br&gt;
    credentials : true,&lt;br&gt;
    optionsSuccessStatus: 200,&lt;br&gt;
    methods: ['GET', 'POST', 'PUT',"OPTIONS"],&lt;br&gt;
    allowedHeaders: ['Content-Type', 'Authorization'],&lt;/p&gt;

&lt;p&gt;}));&lt;br&gt;
cookie result&lt;br&gt;
result.cookie('test', "123", {&lt;br&gt;
                httpOnly: false,&lt;br&gt;
                secure: false,&lt;br&gt;
                sameSite: "None",&lt;br&gt;
                expires: new Date(Date.now() + 3600000),&lt;br&gt;
                maxAge: 3600000,&lt;br&gt;
            });&lt;br&gt;
result.status(201).send({&lt;br&gt;
                success: true,&lt;br&gt;
                message: "login successful"&lt;br&gt;
            });&lt;/p&gt;

&lt;p&gt;The following is my frontend React program&lt;br&gt;
fetch('&lt;a href="http://192.168.0.172/login" rel="noopener noreferrer"&gt;http://192.168.0.172/login&lt;/a&gt;', {&lt;br&gt;
  method: 'POST',&lt;br&gt;
  credentials: 'include', // 包含 cookie&lt;br&gt;
  headers: {&lt;br&gt;
    'Content-Type': 'application/json'&lt;br&gt;
  },&lt;br&gt;
  body: JSON.stringify({ username: 'test', password: '123' })&lt;br&gt;
})&lt;/p&gt;

&lt;p&gt;When I initiate a request, the backend returns a success, and the cookie can be seen on the frontend web page, but the cookie is not obtained using cookies.get.&lt;br&gt;
Why can I successfully obtain the cookie headers when I use fetch without react?&lt;br&gt;
Are the cookies returned by the backend blocked? How can I successfully retrieve the cookies from the backend when using react?&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
