使用以下Stack Express、Vue、SQL、Axios
<form action="url" method="POST">
它工作正常,数据存储在数据库中。
我尝试过很少的解决办法,比如在postman中禁用SSL设置,并使用代理设置,也在后端启用CORS,并尝试了一些允许内容和标题内容。什么都没起作用
无法解决邮件请求中的问题。请帮帮忙
-从Axios请求浏览器中的错误
执行POST请求时出现-postman错误- 邮递员错误
-后端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"));
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
.post("/product/add", formData)
.then((res) => console.log(res))
.catch((error) => console.log(error));
发布于 2020-08-13 05:48:59
我得到了答案,我在index.js中缺少了一个中间件index.js,因此数据库中没有值,因此出现了网络错误。
发布于 2020-08-12 04:58:00
我意识到,您正在向后端发送PUT请求,但是API控制器接收POST请求。所以,他们不是必须是同一类型的请求吗?
https://stackoverflow.com/questions/63376863
复制相似问题