$.get/post
サーバーサイドスクリプトのようなことをしたいだけです。jQuery全体を含める代わりに、より良い方法はありますか? 私は面倒な get xml http requests を手動で使用したくありません。
2 に答える
3
node.js の jquery.ajax に相当するのはrequestです
これは、ノード コア http の上にあり、作業をより快適にします。コールバックとストリーミング リクエストの両方を許可します。
例:
var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Print the google web page.
}
})
request.get('http://google.com/img.png').pipe(request.put('http://mysite.com/img.png'))
于 2013-03-28T05:55:27.823 に答える
2
httpが必要です。ドキュメントにあるように、次のようなリクエストを行うことができます。
var options = {
hostname: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
単純なGETもあります。
http.get("http://www.google.com/index.html", function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
Express.jsとrequestを調べることもできます。実際には、jquery 以外にも使用できる多くのオプションがあります。
于 2013-03-28T05:43:50.610 に答える