[英]Can't Fetch Data from REST call in BackBone: Uncaught TypeError: Cannot read property 'length' of undefined
我在使用Backbone從REST端點檢索json數據時遇到問題。 它將console.log整個json源,但給我一個JSON輸出中實際值的“未捕獲的TypeError:無法讀取屬性'length'of undefined”錯誤。 我可以使用對REST端點的直接AJAX調用來按需檢索數據,如下所示:
$.ajax({
async: false,
url: "http://myajaxcall.com/articles/featured",
type: "GET",
headers: { "Accept": "application/json;odata=verbose" },
success: function (data) {
$.each(data.aside, function (index, value) {
console.log(value.headLine);
});
}
});
但是我無法讓BackBone獲取相同的數據。 我在哪里出錯?
var featuredArticle = Backbone.Model.extend({
defaults: {
id: '',
headLine: '',
snippet: '',
fullStory: '',
location: '',
nsfw: '',
category: '',
relatedArticleIds: '',
hasVideoPlaceholder: '',
numberOfImages: ''
}
});
var featuredArticles = Backbone.Collection.extend({
model: featuredArticle,
url: 'http://myajaxcall.com/articles/featured'
});
var articleView = Backbone.View.extend({
tagName: 'ul',
render: function () {
var that = this;
var getfeaturedArticles = new featuredArticles();
getfeaturedArticles.fetch({
dataType:"json",
success: function(data) {
console.log(getfeaturedArticles);
$.each(data.aside, function (index, value) {
console.log(value.headline);
$(that.el).append('<li>' + value.headLine + '</li>');
});
}
})
return this;
}
});
var articles = new articleView();
$("body").append(articles.render().el);
回調應如下所示:
success: function(model, response) {
console.log(response);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.