我不太熟悉 Node js 以及处理 http 请求,所以如果这是显而易见的事情,请原谅我。
我正在遵循此 website 上的示例:
$.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);
}
});
我正在尝试在 Node Js 中创建与此等效的内容。我尝试使用“Node 请求”,但我的代码无法正常工作。我尝试了很多变体,但都无济于事。
这是一个例子:
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);
});
当我编译此代码时,我的终端回复:
出了点问题。我们已收到通知。
最佳答案
使用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)
});
});
关于javascript - $.ajax调用node js等效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37689446/