如果我放入浏览器地址栏http:// localhost:3000 / api / me,我会返回index.html页面,但如果我使用fetch API来获取/ api / me,它会尝试通过后端调用.
问题是我必须使用后端进行身份验证,后端会设置一个cookie,但由于我无法访问http:// localhost:3000 / login上的登录页面,我无法获取cookie.
在我从create-react-app中弹出的另一个项目中,我有一个小文件来运行webpack-dev-server和配置
proxy: { "*": "http://localhost:9191" }
即使放入浏览器地址栏,它也会发出代理请求.
是否可以在create-react-app中创建此类设置?
For single page apps,we generally want to fallback to /index.html.
However we also want to respectproxy
for API calls.
So ifproxy
is specified,we need to decide which fallback to use.
We use a heuristic: if requestaccept
s text/html,we pick /index.html.
Modern browsers include text/html intoaccept
header when navigating.
However API calls likefetch()
won’t generally accept text/html.
If this heuristic doesn’t work well for you,don’t useproxy
.
在REST控制台扩展中运行http:// localhost:3000 / api / me的GET会返回正确的结果.
关于Fetch API and cookies的进一步阅读揭示我必须包括参数凭证:true以发送cookie:
fetch('/api/me',{ credentials: 'include' })
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。