2010-12-16 146 views
4

我怎麼能發送帶有數據POST請求(例如,一些變量),以HTTPS服務器並顯示結果給最終用戶?POST請求

+0

使用AJAX! (另外,請提供你想要做什麼:)一些更詳細) – 2010-12-16 11:34:28

+1

因此,這Node.js的腳本在這兩個中間的服務器上運行? – thejh 2010-12-16 11:35:28

+0

是實現一些休息的API,並進行身份驗證,我需要徵得他的許可令牌API服務器,我需要使用POST請求的一些祕密ID,因此無法使用AJAX(它必須在服務器端完成,無需任何客戶端集成) – qqryq 2010-12-16 11:38:48

回答

4

使用http.Client模塊。從文檔:

var http = require('http'); 
var google = http.createClient(80, 'www.google.com'); 
var request = google.request('GET', '/', {'host': 'www.google.com'}); 
request.end(); 
request.on('response', function (response) { 
    console.log('STATUS: ' + response.statusCode); 
    console.log('HEADERS: ' + JSON.stringify(response.headers)); 
    response.setEncoding('utf8'); 
    response.on('data', function (chunk) { 
    console.log('BODY: ' + chunk); 
    }); 
}); 

我敢肯定,你可以用GET交換POST;)

+0

確定,以及如何將數據送到這裏?我知道我必須在https的createClient()中將'security'arg設置爲'true',但我不知道如何發送數據。 – qqryq 2010-12-16 11:47:04

+0

@qqryq:使用'request.write'。 'request'是一個'http.ClientRequest'對象。 [docs](http://nodejs.org/api.html#http-clientrequest-196)會告訴你一切你需要的。 – jwueller 2010-12-16 11:50:00

+0

好的,但它似乎仍然不工作。 – qqryq 2010-12-18 22:04:30