【问题标题】:node.js - sending an integer as the input parameter in POST requestnode.js - 在 POST 请求中发送一个整数作为输入参数
【发布时间】:2016-06-23 17:44:37
【问题描述】:

我正在尝试从用户(.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。

【问题讨论】:

    标签: javascript json node.js post pug


    【解决方案1】:

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

    parseInt(arg);
    

    【讨论】:

      【解决方案2】:

      使用 parseInt 将其转换为整数,你应该这样做

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

      【讨论】:

        猜你喜欢
        • 2016-08-02
        • 2016-09-03
        • 1970-01-01
        • 2022-01-04
        • 1970-01-01
        • 1970-01-01
        • 2019-02-02
        • 2013-09-03
        • 2021-07-27
        相关资源
        最近更新 更多