我的react应用程序运行在localhost:3000上,在我的代码中使用fetch到localhost:5000/users发出一个GET请求。
const [users, setUsers] = useState([]);
useEffect(() => {
fetch('http://localhost:5000/users')
.then(res => res.json())
.then(data => setUsers(data))
}, []);但我收到了一个错误
从源'http://localhost:5000/users‘获取'http://localhost:3000’的访问已被CORS策略阻止:请求的资源上没有“访问控制-允许-原产地”标题。
发布于 2022-05-01 17:23:47
这是一个被广泛讨论的问题。按照以下步骤执行:
如果您使用nodejs或express js作为后端。在您的快递应用程序中安装cors包。
npm install cors并且,在您的index.js文件中包括以下内容。
var cors = require('cors')
app.use(cors())就这样。现在应该管用了。不要忘记重新启动节点服务器。
要更好地理解它,请阅读此文章。
发布于 2022-04-30 17:36:47
这是个CORS问题,试着用这种方法
fetch('http://localhost:5000/users', { mode: 'no-cors' })https://stackoverflow.com/questions/72070493
复制相似问题