【问题标题】:Sending POST data to a page with a GET parameter with Nodejs使用 Nodejs 将 POST 数据发送到带有 GET 参数的页面
【发布时间】:2012-04-16 19:17:29
【问题描述】:

我正在尝试将 POST 数据发送到 test.php 文件,该文件仅在提供特定 GET 数据时才处理 POST 数据。不幸的是,我不知道该怎么做;并且已经搜索了大约一个小时。

任何帮助将不胜感激,因为我目前正在玩这个神奇的东西。

提前致谢;

一些澄清:

假设您有一个如下所示的 index.php:

<?php

if (isset($_GET['p']))
    echo count($_POST) . ' -- ' . count($_GET);
else echo 'fuuu';

?>
<form action="?p" method="POST">
    <input type="submit" name="lolw" value="Go" />
</form>

如果您提交该表单,PHP 的 $_GET$_POST 超全局变量都将包含 1 个元素。

现在,让我们尝试通过 nodeJS 运行该表单。

这是我的测试用例(这只是从 doku 中的一些剪切粘贴):

var http = require('http');

var options = {
   hostname: 'localhost',
   port: 80,
   path: '/test.php?lolw=1&p',
   method: 'POST'
};

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  }).on('error', function (e) {
    console.log('error in chunk');
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.toString());
});

// write data to request body
req.write('data\n');
req.end();

CLI 输出为正文提供: 0 -- 2 然后跟在表格后面。

我的观点是:是否可以通过 GET 发送 一些 参数,通过 POST 发送其他一些参数,并指定哪些需要通过 GET 发送,哪些需要通过 POST 发送?

【问题讨论】:

  • 毫无意义。请澄清您的问题或邮政编码
  • 以上说的毫无意义。需要详细说明吗?
  • 为清楚起见而编辑,对此深表歉意。

标签: forms node.js post get


【解决方案1】:

要在 node.js 中发出 HTTP 请求,您需要使用 http.request。您可以将方法设置为“POST”,并且在您的 URL 中仍然有获取参数。对于“标准”POST 请求,您需要将 Content-Length 标头设置为数据长度,并将 Content-Type 标头设置为 application/x-www-form-urlencoded

你的问题很模糊。如果您遇到特定问题,只需显示您的代码并提出更准确的问题即可。

【讨论】:

猜你喜欢
  • 2016-01-10
  • 2014-03-27
  • 2021-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多