[英]Converting ajax to node.js
我已经学会了处理ajax调用以将信息从服务器交换到浏览器的方法,但是现在我遇到了很大的麻烦,需要使用http请求将代码转换为服务器端节点兼容的JS。 我已经阅读了不同的教程,但我只是无法使它们适应我的代码。 我简单的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);
},
});
}
而且我需要将其转换为带有http请求的纯JS版本,以与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.
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.