提问者:小点点

Axios Vue应用程序仅Post请求网络错误


使用以下堆栈Express,Vue,SQL,Axios

  1. 从Axios获取请求在邮递员中也正常工作
  2. POST请求在两个附加的屏幕截图中都创建了错误
  3. 为了确保后端工作正常,我尝试直接从以下位置发送数据
<form action="url" method="POST"> 

它工作正常,数据存储在数据库中

我已经尝试了一些变通方法,比如在邮递员中禁用SSL设置,玩代理设置,在后端启用CORS,并尝试了一些允许内容和标题的东西。 什么都没起作用

无法解决POST请求中的问题。 请帮帮忙

--浏览器中来自Axios的请求错误----
Axios浏览器错误

-执行POST请求时邮递员错误---邮递员错误

---后端index.js文件---

// const express = require("express");
"use strict";

import express from "express";
const app = express();
import cors from "cors";

//listening on this port
app.listen(3000);

app.use(cors()); // to get Data from Other Domains

app.get("/", function (req, res) {
  res.send("Application Started");
  console.log("Application Started");
});

//Routes
app.use("/product", require("./routes/product"));

---product.js路由文件---

import express from "express";
const router = express.Router();
import bodyParser from "body-parser";

//Middleware
router.use(bodyParser.urlencoded({ extended: true })); // To Parse the body data

//Importing Main Controller
import conProduct from "../controllers/ConProduct";

//Defining functions as per Routes
router.post("/add", conProduct.add); //Showing all the products
router.get("/get", conProduct.get); //Showing all the products

//Exporting Router
module.exports = router;

---产品文件conproducts.js的控制器---

import sqlConfig from "../database/dbConfig";
let sql = sqlConfig.mysql_pool;

exports.add = (req, res) => {
  console.log(req.body);
  const { name, shortDescription, price, discount } = req.body;

  let addQuery =
    "INSERT INTO products(name,short_description,price,discount) VALUES('" +
    name +
    "','" +
    shortDescription +
    "','" +
    price +
    "','" +
    discount +
    "');";

  sql.query(addQuery, (err, result) => {
    if (err) throw err;
    console.log(result);
    res.send("product uploaded");
  });
};

--前端axios请求--

let formData = {
        name: this.name,
        shortDescription: this.shortDescription,
        price: this.price,
        discount: this.discount,
      };
      console.log(formData);
      axios
        .put("/product/add", formData)
        .then((res) => console.log(res))
        .catch((error) => console.log(error));

共1个答案

匿名用户

我意识到,您正在向后端发送PUT请求,但您的API控制器接收POST请求。 所以,他们不是必须是同一类型的请求吗?