2015-02-05 4 views
0

nodej를 통해 일부 API 게시물 데이터를 개발 중입니다. 문제가 생겼어. https를 통해 데이터를 게시하면 PHP는 빈 데이터를 처리합니다. 누구든지 알고 있습니까? 감사합니다.nodejs ssl 연결을 통해 빈 데이터 게시

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); 

답변

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) 
    } 
}) ` 
관련 문제