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

node.js - sending an integer as the input parameter in POST request

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

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。

您可以像这样在服务器端作为字符串传递并转换为 int:

parseInt(arg);

使用 parseInt 将其转换为整数,如果你的情况你应该这样做

var empid=parseInt(req.body.empid).