Problem: The front-end clearly passed the parameters, and the back-end could not get the value requested by the front-end through the post request.
Backend solution one:
Use the built-in middleware that comes with express in app.js to solve
const express = require("express");
const app = express();
// 解析 url-encoded格式的表单数据
app.use(express.urlencoded({ extended: false }));
// 解析json格式的表单数据
app.use(express.json());
Backend solution two:
Install and use body-parser in app.js to solve
const express = require("express");
const app = express();
// 导入 body-parser中间件解析表单数据
const bodyParser = require("body-parser");
// 解析 url-encoded格式的表单数据
app.use(bodyParser.urlencoded({ extended: false }));
// 解析json格式的表单数据
app.use(bodyParser.json());
At this point, some friends may say, why can’t I get the value of the front end after doing all the backend, as follows: the front end also needs to be operated
Front-end solution:
Add 'application/json;charset=utf-8' to the front-end request header 'Content-Type'
// 请求拦截器
beforeRequest(method) {
method.config.headers['Content-Type'] = 'application/json;charset=utf-8'
method.config.headers['Access-Control-Allow-Origin'] = '*'
method.config.headers['Access-Control-Allow-Methods'] = 'PUT, POST, GET, DELETE'
method.config.headers['Access-Control-Allow-Private-Network'] = true
},
Test success:
This is the end of the article, I hope it will be helpful to you~