我需要找到一种方法来获取 Youtube JSON url 并打印标题和描述。当我尝试获取时,我在这里的代码成功,但是当我尝试在控制台中查看它时它接收到的数组是空的。
你知道为什么会发生这种情况吗?
你可以在这里找到代码:http: //jsfiddle.net/BHrmC/73/
var Item = Backbone.Model.extend();
var List = Backbone.Collection.extend({
model: Item,
url: "https://gdata.youtube.com/feeds/api/playlists/67DEB98D8D9CF0F7?v=2&alt=json-in-script&max-results=6",
parse: function(response) {
return response.results;
},
sync: function(method, model, options) {
var that = this;
var params = _.extend({
type: 'GET',
dataType: 'jsonp',
url: that.url,
processData: false
}, options);
return $.ajax(params);
}
});
var ListView = Backbone.View.extend({
el: $('#test'),
events: {
'click button#add': 'getPost'
},
initialize: function() {
_.bindAll(this, 'render', 'getPost');
this.collection = new List();
this.render();
},
render: function() {
var self = this;
$(this.el).append("<button id='add'>get</button>");
},
getPost: function() {
var that = this;
this.collection.fetch({
success: function(response) {
console.log(that.collection.toJSON());
console.log("working");
},
error: function() {
console.log('Failed to fetch!');
}
});
}
});
// **listView instance**: Instantiate main app view.
var listView = new ListView();