簡體   English   中英

如何發送HTTP請求以將node.js中的表單提交到另一個網站?

[英]how to send HTTP request to submit form in node.js to another website?

我想提交其他網站的表格並希望得到答復。 但是不知道如何做到這一點。 誰能幫幫我嗎 ?

我的目標網站: http://xyz.php

我的代碼:

var http = require('http');
var options = {
  host: 'xyz.com',
  path: '/xyz.php?textSearch=234567'
};

callback = function(response) {
  var str = '';
  response.on('data', function (chunk) {
    str += chunk;
  });
  response.on('end', function () {
    console.log(str);
  });
}

http.request(options, callback).end();

您應該使用POST請求,因為表單標記包含方法屬性

 <form id="frmThis" name="frmThis" method="post" action="">

試試這個代碼:

var querystring = require('querystring');
var http = require('http');

var post_data = querystring.stringify({
    txtSearch: "17051017387"
})

var options = {
    host: 'nbr.gov.bd',
    path: '/getbinfield.php',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': post_data.length
    }
};

callback = function(response) {
    console.log(arguments);
    var str = '';
    response.on('data', function (chunk) {
        str += chunk;
    });
    response.on('end', function () {
        console.log(str);
    });
}

var req = http.request(options, callback)
req.write(post_data)
req.end()

使用請求模塊:

var request = require('request')
request.post('http://nbr.gov.bd/getbinfield.php', {
  form: {
    txtSearch: '...search for something',
    bthSubmit: 'Search'
  }
}, function (err, res, body) {
  console.log(body)
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM