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

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

本文关键字:POST 请求 参数 输入 整数 一个 js node      更新时间:2023-09-26

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

应用.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 将其转换为整数

parseInt(empid).