我正在尝试从网站调用API并打印出JSON结果。 但我看不出结果。 有任何想法弄清楚这个问题。 谢谢。
var http = require('http');
var data_info="";
http.createServer(function (req, res) {
sendJsontoAlchemy(outputMode());
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('end\n');
}).listen(1111)
console.log('Server is on');
function sendJsontoAlchemy()
{
requestNumber = JSONRequest.post(
"http://access.alchemyapi.com/calls/text/TextGetCategory",
{
apikey: "aaaaaa",
text : "Skateboard Mens Trousers t Shirt Wooden Fence",
outputMode : json,
jsonp:outputMode//call back function.
},
function (requestNumber, value, exception) {
if (value) {
processResponse(value);
} else {
processError(exception);
}
});
}
function outputMode(response)
{
console.log("the result is =>");
console.log(JSON.stringify(response));
}
答案 0 :(得分:0)
我建议使用非常有用的模块request来做这些事情。
示例代码:
var request = require('request');
request({
method: 'POST',
uri: 'http://access.alchemyapi.com/calls/text/TextGetCategory',
body: 'apikey=1337abcd'+
'&text=Skateboard Mens Trousers t Shirt Wooden Fence' +
'&outputMode=json',
}, function(err, res, body){
result = JSON.parse(body);
console.log('Category: ', result.language);
console.log('Category: ', result.category);
});
如果您需要从服务器输出该东西,您可以在服务器内执行请求({opts})。pipe(响应),而不是指定回调。
var http = require('http');
var request = require('request');
http.createServer(function(req, res){
request({
method: 'POST',
uri: 'http://access.alchemyapi.com/calls/text/TextGetCategory',
body: 'apikey=1d416bcdde7cf8a360fad31701ac2c2ba57bc75f'+
'&text=Skateboard Mens Trousers t Shirt Wooden Fence' +
'&outputMode=json',
}).pipe(res);
}).listen(1111);