$ .ajax调用节点js等价吗?

我不是很熟悉Node js,也不太熟悉http请求,所以如果这是显而易见的,请原谅我。

我正在关注这个网站上的例子:

$.ajax({ url: 'https://api.wit.ai/message', data: { 'q': 'set an alarm in 10min', 'access_token' : 'MY_WIT_TOKEN' }, dataType: 'jsonp', method: 'GET', success: function(response) { console.log("success!", response); } }); 

我正在尝试创build相当于此,但在节点Js。 我试图使用“节点请求”,但是我的代码不工作。 我尝试了很多这个变化,但没有用。

这里是一个例子:

 var request = require('request'); var url = 'https://api.wit.ai/message'; var data = { 'q': 'hello test123 trying to get entities from this message', 'access_token': 'MY_WIT_TOKEN' }; request.get({ url: url, formData: data }, function (err, httpResponse, body) { if (err) { return console.error('post failed:', err); } console.log('Get successful! Server responded with:', body); }); 

当我编译这个代码时,我的terminal回复:

出了些问题。 我们已收到通知。

使用http :

 var http = require('http'); http.get({ host: 'api.wit.ai', path: '/message' }, function(response) { var body = ''; // get all data from the stream response.on('data', function(data) { body += data; }); response.on('end', function() { // all data received console.log(body) }); }); 

对任何感兴趣的人来说,都是使用节点请求的答案。

 var request = require('request'); var headers = { 'Authorization': 'Bearer <WIT_TOKEN>' }; var options = { url: 'https://api.wit.ai/message?v=20160607&q=hello', headers: headers }; function callback(error, response, body) { if (!error && response.statusCode == 200) { console.log(body); } } request(options, callback);