【发布时间】:2021-02-11 23:27:21
【问题描述】:
我无法使用 fetch 或 Postman 发送帖子正文,但我可以使用 Axios 发送正文。
Axios 返回:User name = Fred, password is Flintstone
但是 Postman 和 fetch 都返回 User name = undefined, password is undefined
如何使用 fetch 和 postman 获取帖子正文?
服务器文件
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const axios = require("axios");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post("/login", (req, res) => {
var user_name = req.body.user;
var password = req.body.password;
console.log("User name = " + user_name + ", password is " + password);
res.end("yes");
});
app.listen(3000, () => {
console.log("Started on PORT 3000");
});
Axios
axios.post("http://localhost:3000/login", {
user: "Fred",
password: "Flintstone",
});
获取(客户端)
fetch("http://localhost:3000/login", {
method: "POST",
body: JSON.stringify({ user: "Fred", password: "Flintstone" }),
mode: "no-cors",
});
邮递员
【问题讨论】:
标签: javascript node.js axios fetch postman