将ajax转换为node.js

我已经学会了处理Ajax调用来从服务器交换信息到浏览器,但现在我有麻烦转换我的代码到服务器端节点兼容JS使用http请求。 我读过不同的教程,但我不能适应他们到我的代码。 我简单的JS / jQuery函数是这样的:

function ajaxCall(data, php, callback) { ax = $.ajax({ type: "POST", data: data, url: php, success: function (raw_data) { f = $.parseJSON(raw_data); callback(f); }, }); } 

我需要将其转换为纯JS版本,并使用http请求与node.js一起使用。 谢谢你的帮助。

编辑 :我试过,但没有成功。 这里是我使用的代码,我只是在我的console.log上得到了许多毫无意义的单词,也许你可以纠正它:

版本1

 var data = {"action": "update", "tN": 2155}; var request = require("request"); request.post({ url: 'http://example.com/PHP.php', data: data, }, function(error, response, body){ console.log(body); } ); 

版本2

 var request = require("request"); var options = { method: 'POST', uri: 'http://example.com/PHP.php', data: {"action": "update", "tN": 2155}, }; request(options, function(error, response, body) { if(error){ console.log(error); }else{ console.log(response); } }); 

使用request.js

例:

 var request = require('request'); request('http://www.google.com', function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body) // Show the HTML for the Google homepage. } }) 

文档request.js