Use Fetch API to pass cookies
P粉066725148
P粉066725148 2023-08-21 17:18:31
0
2
582
<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>
P粉066725148
P粉066725148

reply all(2)
P粉106711425

In addition to @Khanetor's answer, for those who are dealing with cross-origin requests, you can use credentials: 'include'

Example JSON fetch request:

fetch(url, {
  method: 'GET',
  credentials: 'include'
})
  .then((response) => response.json())
  .then((json) => {
    console.log('Gotcha');
  }).catch((err) => {
    console.log(err);
});

https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials

P粉818088880

By default, Fetch does not use cookies. To enable cookies, do the following:

fetch(url, {
  credentials: "same-origin"
}).then(...).catch(...);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!