【问题标题】:Redirect to new page and set POST data重定向到新页面并设置 POST 数据
【发布时间】:2014-01-11 15:47:46
【问题描述】:

我正在尝试重定向到新网站并设置要发送到该网站的帖子数据。我试过这个:

var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(302, {"Location": "http://example.com/newpage/", "Content-Type": "application/x-www-form-urlencoded"});
    res.end("param1=value1&param2=value2");
}).listen(process.env.PORT, process.env.IP);

这不成功。如何重定向到新网站并设置 POST 数据?

【问题讨论】:

    标签: javascript node.js redirect


    【解决方案1】:

    简单重定向不能与发布数据一起发送。 您必须提出新请求并通过管道传输到原始请求

    http.createServer(function (req, res) {
      var http = require('http');
    
      var post_data = JSON.stringify({aaa: 'abc', bbb: 123});
    
      var options = {
        host: 'example.com',
        port: '80',
        path: '/',
        method: 'POST',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Content-Length': post_data.length
      };
    
    
      var req = http.request(options, function(resp_new) {
        resp_new.setEncoding('utf8');
    
        resp_new.on('data', function(chunk){
          res.send(chunk);
        });
    
        resp_new.on('end', function(){
          res.end();
        });
      }); 
    
      req.write(post_data);
      req.end();
    });
    

    【讨论】:

      猜你喜欢
      • 2010-11-21
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-17
      • 2011-08-03
      相关资源
      最近更新 更多