小编You*_*sef的帖子

空的请求正文

我生成了两个项目,一个使用create-react-app,另一个使用express generator。

我先运行第一个,localhost:3000然后运行第二个localhost:3001

我正在尝试发送POST请求,但是收到一个empty req.body

客户端:

 handleSubmit(event) {
    event.preventDefault();
    var data = new FormData();
    for (const key of Object.keys(this.state)) {
      data.append(key, this.state[key]);
    }

    const url = "http://localhost:3000/course";
    fetch(url, {
      method: "POST",
      body: this.state
    })
      .then(response => response.text())
      .then(html => console.log(html));
  }
Run Code Online (Sandbox Code Playgroud)

服务器端:

app.js

app.use(logger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  next();
});
app.use("/course", course);
Run Code Online (Sandbox Code Playgroud)

router/course.js

router.post("/", function(req, res) {
  if …
Run Code Online (Sandbox Code Playgroud)

node.js express reactjs body-parser

3
推荐指数
1
解决办法
2240
查看次数

标签 统计

body-parser ×1

express ×1

node.js ×1

reactjs ×1