【发布时间】:2015-01-22 04:23:31
【问题描述】:
我已经学会了处理 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);
}
});
【问题讨论】:
-
编辑:我确实不想在 node.js 中使用 jquery 模块(这很成问题,尤其是在 Windows 上!)
-
所以你不是真的想要ajax,你只是想从服务器发出请求?
-
如果你不想要 XHR 但只需要发出一个 http 请求请看这里stackoverflow.com/questions/25652057/…
标签: javascript jquery ajax node.js