node.js - 在 POST 请求中发送一个整数作为输入参数

问题描述 投票:0回答:2

我正在尝试从用户(.jade 页面)输入一个整数并将该值作为 body 参数传递。当我尝试使用下面的代码时,该值被转换为字符串。你能帮我吗?

app.js

 var empid = req.body.empid;  // getting value from the jade page
 console.log(empid); // output here is 1111
 var requestdata = {1:empid};
 console.log(requestdata); // output here is '1111'
  var options = {
    url: my URL goes here,
    method: 'POST',
    headers: {
          'Content-Type': 'application/json'
      },
    auth: {
    user : username,
    pass : '****'
          },
    body: JSON.stringify(requestdata)
}

当我尝试将其传递到 POST 请求时,我希望该值为 1111。

javascript json node.js post pug
2个回答
2
投票

您可以作为字符串传递并在服务器端转换为 int,如下所示:

parseInt(arg);

0
投票

使用 parseInt 将其转换为整数

parseInt(empid).
© www.soinside.com 2019 - 2024. All rights reserved.