簡體   English   中英

嘗試從 localhost:8080 向 localhost:3000 發出 GET 請求,但我收到 cors 錯誤,並在我的 node.js 服務器上安裝 cors 無法修復它

[英]Trying to make a GET request from localhost:8080 to localhost:3000 but I get a cors error, and installing cors on my node.js server doesn't fix it

因此,我試圖從localhost:8080上的前端應用程序向我的 Node.js/Express.js localhost:3000服務器發出 axios GET 請求,我明白為什么我會收到 CORS 錯誤。 What I don't understand is why after I installed the cors npm package and used it as middleware on my Node.js/Express.js back-end, I still get the CORS error.

我在app.js文件中做了這個

var cors = require('cors');
app.use(cors());

然后,當我嘗試從前端發出 GET 請求時,仍然出現以下錯誤:

Access to XMLHttpRequest at 'http://localhost:3000/images' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我不應該使用 cors package 解決這個問題嗎?

您必須允許 cors 接收該來源。 有一個關於這個的好視頻 但是將參數 origin 添加到 cors() 中間件應該可以解決它。

var cors = require('cors');
app.use(cors({
    origin: "http://localhost:8000"
}));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM