如何在http请求方法中传递帖子数据

时间:2018-02-22 16:42:01

标签: json node.js http post

我的代码执行时会出现以下错误提示 似乎无法读取<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr class='mui-row'> <td class='mui-col-md-6'>1</td> <td class='mui-col-md-2'>1</td> <td class='mui-col-md-2' style='border:none'> <input type="checkbox" class="check"> </td> <td>editButton</td> </tr> <tr class='mui-row'> <td class='mui-col-md-6'>2</td> <td class='mui-col-md-2'>2</td> <td class='mui-col-md-2' style='border:none'> <input type="checkbox" class="check"> </td> <td>editButton</td> </tr> <tr class='mui-row'> <td class='mui-col-md-6'>3</td> <td class='mui-col-md-2'>3</td> <td class='mui-col-md-2' style='border:none'> <input type="checkbox" class="check"> </td> <td>editButton</td> </tr> </table>参数。

  

缺少必需的输入JSON参数requestType。

body

1 个答案:

答案 0 :(得分:0)

我为request方法检查了节点的http模块最新documentation

它的options参数不接受任何body属性

还有一个示例表明您需要在写入方法

中传递body
const postData = JSON.stringify({
      requestType: 'createService'
    });

const options = var options = {
    host: 'hostname',
    port: 80,
    path: '/service',
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Authorization': "Basic " + new Buffer(username + ":" + pass).toString("base64")
    }
  };

const req = http.request(options, (res) => {
  console.log(`STATUS: ${res.statusCode}`);
  console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    console.log(`BODY: ${chunk}`);
  });
  res.on('end', () => {
    console.log('No more data in response.');
  });
});

req.on('error', (e) => {
  console.error(`problem with request: ${e.message}`);
});

// write data to request body
req.write(postData);
req.end();