javascript - 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
谢谢
解决方案
检查服务器是否确实在返回数据。您可以控制台记录 res 并在控制台中检查对象。
推荐阅读
- javascript - Vue axios 错误 Uncaught (in promise) 错误:请求失败,状态码为 415
- asp.net-mvc - 将值从锚点传递到控制器
- firebase - 生成密码重置链接 Firebase 时,HandleCodeInApp false 未重定向到 Web 浏览器
- jmeter - 尝试对使用 OJET 技术开发的应用程序进行性能测试。我应该使用哪种工具/协议来编写脚本?
- asp.net-core - 如果不满足条件,则查看数据 Blazor
- reactjs - 我如何查看为什么 Axios POST 请求没有将有效负载数据发送到 Heroku 后端?
- php - 我可以使用这个检测人类与机器人访问者吗
- c# - 我应该在哪里将动画标志 bool 设置回 false?
- c++ - 这两个语句是什么意思 char (*test)[10]; 测试=新字符[4][10];
- javascript - 如何通过单击其他缩略图来更改主图像