首页 > 解决方案 > 为什么在获取 API 数据时会出现 403 禁止?

问题描述

react-admin在前端使用来访问已gin开发的 API 后端。

从 chrome 浏览器访问 url 时:

http://localhost:3000/#/posts

从 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

标签: reactjsapigwt-gin

解决方案


由于前端和后端位于不同的来源(分别为 localhost:3000 和 localhost:8080),因此您需要在后端设置 CORS 以允许 localhost:3000 访问它。

https://github.com/gin-contrib/cors看起来像是门票。


推荐阅读