【问题标题】:Axios Network Error depending on CORS policy in Node JSAxios 网络错误取决于 Node JS 中的 CORS 策略
【发布时间】:2022-06-22 18:53:14
【问题描述】:

尝试执行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))
    };

那么为什么会出现这些错误呢?为什么网络面板中没有显示标题?

【问题讨论】:

    标签: javascript node.js express cors


    【解决方案1】:

    错误消息没有说明 CORS。

    它的意思是在带有“广告”的 URL 上显示“被客户端阻止”,这强烈表明问题出在浏览器中安装了广告阻止扩展扩展。

    【讨论】:

      猜你喜欢
      • 2021-04-19
      • 2020-12-18
      • 2021-12-25
      • 2020-08-27
      • 2021-02-22
      • 2021-08-14
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多