首页 > 解决方案 > axios POST 请求未在控制台中触发响应

问题描述

刚刚开始从事 MERN 项目。所以我从客户端发送这个 POST 请求。这是代码

onSubmit(e) {
  e.preventDefault();
  const user_info = {
    username:this.state.username,
    password:this.state.password,
  }
 axios.post('http://localhost:5000/weather/usercreated',user_info)
 .then((res)=>{console.log(res.data)}).catch((err)=>{console.log(err)});
}

因此,从我从后端看到的情况来看,POST 请求正在运行。但是前端代码的问题是,这部分:

then((res)=>console.log(res.data))

响应未显示在控制台中。我需要一些帮助。

以下是我放在后端的代码

const router = require('express').Router();
const User = require('../models/userid');


let user_info = ''
router.post('/usercreated',(req,res)=>{

    const username = req.body.username
    const password = req.body.password

    user_info = new User({
        username,
        password,
    })

    user_info.save().then(()=>res.json('added'))
})

module.exports=router

谢谢

标签: javascriptreactjs

解决方案


检查服务器是否确实在返回数据。您可以控制台记录 res 并在控制台中检查对象。


推荐阅读