使用 Cookie 获取 API [英] Fetch API with Cookie

查看:24
本文介绍了使用 Cookie 获取 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试用新的 Fetch API,但在使用 Cookie 时遇到了问题.具体来说,登录成功后,以后的请求中会有一个 Cookie 头,但是 Fetch 似乎忽略了那个头,我所有使用 Fetch 发出的请求都是未经授权的.

I am trying out the new Fetch API but is having trouble with Cookies. Specifically, after a successful login, there is a Cookie header in future requests, but Fetch seems to ignore that headers, and all my requests made with Fetch is unauthorized.

是因为 Fetch 还没有准备好还是 Fetch 不支持 Cookies?

Is it because Fetch is still not ready or Fetch does not work with Cookies?

我使用 Webpack 构建我的应用程序.我也在 React Native 中使用了 Fetch,它没有同样的问题.

I build my app with Webpack. I also use Fetch in React Native, which does not have the same issue.

推荐答案

Fetch 默认不使用 cookie.要启用 cookie,这样做::>

Fetch does not use cookie by default. To enable cookie, do this:

fetch(url, {
  credentials: "same-origin"
}).then(...).catch(...);

这篇关于使用 Cookie 获取 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆