reactjs - 为什么在获取 API 数据时会出现 403 禁止?
问题描述
react-admin
在前端使用来访问已gin
开发的 API 后端。
从 chrome 浏览器访问 url 时:
从 chrome 收到这条消息Console
:
fetch.js:41 OPTIONS http://localhost:8080/posts?_end=10&_order=DESC&_sort=id&_start=0 403 (Forbidden)
但是如果从浏览器访问上面的uri,它可以返回json结果。
从gin
开发的 API 输出中,得到以下消息:
[GIN] 2018/06/13 - 16:38:16 | 403 | 1.995µs | ::1 | OPTIONS /posts?_end=10&_order=DESC&_sort=id&_start=0
解决方案
由于前端和后端位于不同的来源(分别为 localhost:3000 和 localhost:8080),因此您需要在后端设置 CORS 以允许 localhost:3000 访问它。
推荐阅读
- python-3.x - 客户端和服务器错误输出
- ag-grid-angular - 在 Agrid 中隐藏过滤器行
- reactjs - 如何使用反应路由器 dom 在另一个组件中渲染一个组件中存在的链接?
- visual-studio-code - 如何在 VSCODE 中删除这些奇怪的工件?
- reactjs - 如何在 GET/POST react js 的 setState 中调用另一个类的函数/方法?
- javascript - Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException- laravel 5.7.28
- ios - 取消 WhatsApp Swift 之类的语音注释
- python - 根据列中的行值对每个组执行扣除
- node.js - 我想使用节点 js 将 mongodb 查询结果保存到 postgresql
- java - 我已经创建了 chrome 驱动程序对象,然后我得到了 java.lang.NullPointerException