嘗試執行 put axios 請求時出現以下錯誤:

CORS 模塊已安裝并在 server.js 檔案中切換,但似乎不起作用。順便說一下,請求中沒有任何 CORS 標頭:

所以在 server.js 檔案中實作了 CORS 模塊。我試圖在括號中添加 {origin: "http://localhost:3000"} 但這沒有用。括號是空的。
server.js:
const express = require("express");
const mongoose = require("mongoose");
const apiRoutes = require('./routes/apiRoutes');
const path = require('path');
const cors = require("cors")
require("dotenv").config();
const app = express();
const PORT = 3001;
app.use(cors());
app.use(express.static(path.join(__dirname, 'images')));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(apiRoutes);
mongoose
.connect(process.env.MONGO_URL)
.then(res => console.log("The connection to the Database was carried out successfully."))
.catch(error => console.log(`The Error while connecting to the Database occured: ${error.message}`))
app.listen(PORT, 'localhost', error =>
console.log(error ? `The Error occured: ${error.message}.` : `The Listening Port is ${PORT}`)
);
路線:
router.put("/api/newAd/:creationDate", (req, res) => {
console.log(req.body);
// Ad.updateOne({ creationDate: req.body.creationDate }, {
// textInfo: req.body
// })
})
客戶端:
const saveAndPlace = () => {
axios.put(`/api/newAd/${creationDate}`)
.catch(err => console.log("error", err))
};
那么為什么會出現這些錯誤呢?為什么網路面板中沒有顯示標題?
uj5u.com熱心網友回復:
錯誤訊息沒有說明 CORS。URL 和Referer 的來源相同,所以它甚至不是跨域請求!
它所做的是在帶有“廣告”的 URL 上顯示“被客戶端阻止”,這強烈表明問題出在瀏覽器中安裝了廣告阻止擴展擴展。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/494414.html
標籤:javascript 节点.js 表示
