Use Fetch API to pass cookies
P粉066725148
2023-08-21 17:18:31
<p>I'm trying to use the new Fetch API and I'm having trouble handling cookies. Specifically, after a successful login, there is a Cookie header in future requests, but Fetch seems to ignore this header and all requests I make using Fetch are unauthorized. </p>
<p>Is this because Fetch is not ready yet, or does Fetch not support Cookies? </p>
<p>I use Webpack to build my application. I'm also using Fetch in React Native and don't have the same problem. </p>
In addition to @Khanetor's answer, for those who are dealing with cross-origin requests, you can use
credentials: 'include'
Example JSON fetch request:
https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
By default, Fetch does not use cookies. To enable cookies, do the following: