2013-05-11 142 views
2

Node中以下瀏覽器端GET請求的等效內容是什麼?GET請求,帶數據

jQuery.ajax({ 
    url: 'http://test.com', 
    type: 'GET', 
    data: myData, 
    success: function(data) { 
     console.log(data); 
    } 
}); 

我最好的嘗試是

http.get('http://test.com', function(response) { 
    response.on('data', function (chunk) { 
     console.log(chunk); 
    }); 
}); 

的問題是我不知道如何在節點版本通過data: myData。我如何將數據傳遞給http.get請求?

回答

4

您必須將完整的URL傳遞給它。您可以通過自己添加查詢參數來創建URL。有一個幫手製作你自己的Query Strings。因此,這將是這樣的:

url = "http://test.com/?" + querystring.stringify(myData); 
http.get(url, ...); 
1

記住get方法是通過URL,這樣你就可以,如果你想

http.get('http://test.com?GetVariable='+myVal, function(response) { 
    response.on('data', function (chunk) { 
     console.log(chunk); 
    }); 
}); 
它添加到URL