一些麻煩我一直在玩弄youtube API和node.js,到目前爲止我已經能夠得到API的響應,並且console.log
它到終端上。節點js - 我遇到了一些JSON.parse()
當我嘗試獲得響應,並使用JSON.parse
,我得到一個奇怪的錯誤:
Got response: 200
undefined:1
http://www.w3.or
^
SyntaxError: Unexpected token u
at Object.parse (native)
at IncomingMessage.<anonymous> (/home/ubuntu/node_temp4/index.js:19:10)
at IncomingMessage.emit (events.js:88:20)
at HTTPParser.onMessageComplete (http.js:137:23)
at Socket.ondata (http.js:1137:24)
at TCP.onread (net.js:354:27)
這是我的腳本:
var http = require("http");
var searchQuery = "cats";
var queryResponse;
var options = {
host: 'gdata.youtube.com',
path: "/feeds/api/videos?q=" + searchQuery + "&max-results=1&v=2&alt=json"
};
http.get(options, function(response) {
console.log("Got response: " + response.statusCode);
response.on('data', function(chunk){
queryResponse += chunk;
});
response.on('end', function(){
JSON.parse(queryResponse);
console.log('end');
});
}).end();
你嘗試分析它之前有什麼queryResponse的價值修復它? – Dogbert 2012-02-18 10:32:56
它會在終端上打印[this](http://gdata.youtube.com/feeds/api/videos?q=cats&max-results=1&v=2&alt=json)。 編輯:一個更可讀的版本 - http://gdata.youtube.com/feeds/api/videos?q=cats&max-results=1&v=2&alt=json&prettyprint=true – user1215653 2012-02-18 10:34:43