私は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端末にログすると、[this](http://gdata.youtube.com/feeds/api/videos?q=cats&max-results=1&v=2&alt=json)が表示されます。 EDIT:読みやすいバージョン - http://gdata.youtube.com/feeds/api/videos?q=cats&max-results=1&v=2&alt=json&prettyprint=true – user1215653