[英]Converting ajax to node.js
I've learnt dealing with ajax calls to exchange information from the server to the browser but now I'm having big troubles converting my code to a server-side node compatible JS using http requests. 我已经学会了处理ajax调用以将信息从服务器交换到浏览器的方法,但是现在我遇到了很大的麻烦,需要使用http请求将代码转换为服务器端节点兼容的JS。 I've read different tutorials but I just can't adapt them to my code. 我已经阅读了不同的教程,但我只是无法使它们适应我的代码。 My simple JS / jQuery function is this: 我简单的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);
},
});
}
And I need to convert it to a pure JS version with http requests to use with node.js. 而且我需要将其转换为带有http请求的纯JS版本,以与node.js一起使用。 Thanks for any help. 谢谢你的帮助。
EDIT : I tried and tried, but without success. 编辑 :我尝试了,但没有成功。 Here is the code I used, I just get lots of meaningless words on my console.log, perhaps you can correct it: 这是我使用的代码,我在console.log上得到了很多毫无意义的单词,也许您可以纠正它:
Version 1 版本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);
}
);
Version 2 版本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);
}
});
Use request.js
使用request.js
Example: 例:
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.
}
})
Documentation request.js 文档request.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.