我有一個緊急問題希望你能盡快幫助我。
我有一個從公司 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
請問我該如何解決這個問題。
uj5u.com熱心網友回復:
CORS 允許從源 http://localhost:1229 獲取內容,因此,嘗試在 http://localhost:1229 上運行開發服務器
PORT=1229 react-scripts start
在 Windows上的 linux上使用命令
set PORT=1229 && react-scripts start
或更改 package.json
"start": "PORT=1229 react-scripts start"
"start": "set PORT=1229 && react-scripts start"
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/352580.html
標籤:javascript 反应 接口
