![](/img/trans.png)
[英]How to fix ''http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.'
[英]Getting "blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource." with MERN stack using Axios
我有一個 MERN 堆棧設置,我的 React 前端在一個服務器實例上,我的后端 Node-Express API 在另一個服務器實例上。 在開發中我沒有問題,但是如果我嘗試在生產中上傳超過1MB的文件,我會收到 CORS 錯誤。 如果它低於 1MB,它上傳就好了。
Node-Express 中間件:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
}); // I have tried without this as well.
app.use(fileUpload({
createParentPath: true,
limits: {
fileSize: 64 * 1024 * 1024 * 1024 // 64MB max file(s) size
},
})) / I have tried without the limits, too.
app.use(cors()); // I have tried even by including the origin parameter
反應
axios.post(`${apiEndpoint}/upload`, data, {
headers: { "x-auth-token":token }
})
.then((response) => console.log(response.data));
感謝評論中很棒的人,有人指出我應該檢查日志。 出於某種原因,像菜鳥一樣,我沒想過要這樣做。 一旦我這樣做了,我發現 nginx 沒有被配置為更大的文件上傳大小。 我將它設置為 100Mb,它就像一個魅力。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.