最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Axios post params没有发送

IT培训 admin 6浏览 0评论

Axios post params没有发送

我在反应应用程序中使用Axios来访问api。我正在尝试将登录详细信息发布到服务器,但是当我记录请求的主体时,我得到的是qazxsw poi。

继承人请求:

{}

如果我删除大括号,只是发送了一个像handleSubmit() { let email = this.state.email let password = this.state.password request.post("/login",{email: email, password: password}) //just prints {} on the backend .then((results)=>console.log(results)) .catch((err)=>console.log(err)) } 这样的字符串作为测试我得到了hello这么清楚它的工作。我已经在这个阶段尝试了一切,以便在现场获得用户输入但是没有成功。

下面是服务器代码:

{ hello: '' }

我的api是使用express.js在端口4000上的节点服务器

回答如下:

更新:必须将对象字符串化为查询字符串。您可以使用var app = express(); var bodyParser = require("body-parser"); app.use(bodyParser.urlencoded({ extended: true })); app.post("/login",(req,res) => { console.log(req.body); }) 来实现此目的。请参阅更新的代码。

由于您使用的是qs package的body-parser中间件,因此请求的内容类型需要设置为“x-www-form-urlencoded”。

bodyParser.urlencoded

Axios post params没有发送

我在反应应用程序中使用Axios来访问api。我正在尝试将登录详细信息发布到服务器,但是当我记录请求的主体时,我得到的是qazxsw poi。

继承人请求:

{}

如果我删除大括号,只是发送了一个像handleSubmit() { let email = this.state.email let password = this.state.password request.post("/login",{email: email, password: password}) //just prints {} on the backend .then((results)=>console.log(results)) .catch((err)=>console.log(err)) } 这样的字符串作为测试我得到了hello这么清楚它的工作。我已经在这个阶段尝试了一切,以便在现场获得用户输入但是没有成功。

下面是服务器代码:

{ hello: '' }

我的api是使用express.js在端口4000上的节点服务器

回答如下:

更新:必须将对象字符串化为查询字符串。您可以使用var app = express(); var bodyParser = require("body-parser"); app.use(bodyParser.urlencoded({ extended: true })); app.post("/login",(req,res) => { console.log(req.body); }) 来实现此目的。请参阅更新的代码。

由于您使用的是qs package的body-parser中间件,因此请求的内容类型需要设置为“x-www-form-urlencoded”。

bodyParser.urlencoded

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论