【发布时间】:2021-11-08 10:22:22
【问题描述】:
我有一个从公司 API 获取产品的测试。
他们告诉我:
CORS 允许的本地开发来源是“http://localhost:1229”
所以我在 package.json 中设置了这样的代理:
"proxy": "http://localhost:1229",
我的要求是:
const apiLink = 'https://example.com/api/checkout/products/';
fetch(apiLink, {
method: 'POST'
}).then(returnedData => {
console.log(returnedData)
}).catch(error => {
console.log(error)
});
但我仍然收到此错误
Access to fetch at 'https://...' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled
【问题讨论】:
-
在这种情况下,您需要将您的
http://localhost:3000作为允许的主机放入您的后端 api。
标签: javascript reactjs api