nodejs通过ssl连接发布空数据

时间:2015-02-05 15:02:13

标签: php node.js ssl https request

我正在通过nodejs开发一些API发布数据。我有些问题。如果我通过https发布数据然后php处理空数据。 有谁知道 ? 谢谢。

a.js =>

//https unauth disable
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';

var http = require('https');

var post_req  = null,
post_data = '{ "dataid":"81","userid":"29" }';

var post_options = {
    hostname: 'www.domain.com',
    port    : '443',
    path    : '/get.php',
    method  : 'POST',
    headers : {
        'Content-Type': 'application/json;',
        'Content-Length': post_data.length
    }
};

debugger;
post_req = http.request(post_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('Response: ', chunk);
    });
});

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

debugger;
post_req.write(JSON.stringify(post_data));
debugger;
post_req.end();

这是php代码块。

get.php =>

print_r($_POST);

1 个答案:

答案 0 :(得分:0)

我修复它:)如果你想使用json请求只需更改内容类型。享受:)

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
var request = require('request');

// Set the headers
var headers = {
    'User-Agent':       'Super Agent/0.0.1',
    'Content-Type':     'application/x-www-form-urlencoded'
}

// Configure the request
var options = {
    url: 'https://www.domain.com',
    method: 'POST',
    headers: headers,
    form: {"dataid":"81","userid":"29" }
}

// Start the request
request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        // Print out the response body
        console.log(body)
    }
}) `