2
我正在爲我的第一個WinJS應用程序的Windows 8,我只是想要替換2網格模板中給出的基本代碼,我越來越這個錯誤:WinJS - 無法獲取屬性'forEach'的未定義或空引用
Unable to get property 'forEach' of undefined or null reference
我的代碼:
getData().forEach(function (item) {
list.push(item);
});
初始功能:
function getData() {
// JSON request
WinJS.xhr({
type: "GET",
url: "http://mytld.com/mobile/pagethatreturnsjsondata.php"
}).then(
function (response) {
OnSuccessCall(response);
},
function (error) {
var err = error;
console.log("Error : " + err.message);
},
function (progress) { }
);
}
以下正從一個PHP Web服務這是返回JSON數據的數據:
function OnSuccessCall(response) {
var itemContent = "<p>Item Content</p>";
var itemDescription = "Item Description";
var groupDescription = "Group Description";
// These three strings encode placeholder images. You will want to set the
// backgroundImage property in your real data to be URLs to images.
var darkGray = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXY3B0cPoPAANMAcOba1BlAAAAAElFTkSuQmCC";
var lightGray = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXY7h4+cp/AAhpA3h+ANDKAAAAAElFTkSuQmCC";
var mediumGray = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXY5g8dcZ/AAY/AsAlWFQ+AAAAAElFTkSuQmCC";
// Each of these sample groups must have a unique key to be displayed separately.
var sampleGroups = [
{ key: "group1", title: "prayers", subtitle: "submitted prayers", backgroundImage: darkGray, description: groupDescription }
];
var x = response.responseText.length;
var json = JSON.parse(response.responseText);
var sampleItems = [];
for (var i = 0; i < json.length - 1; i++) {
sampleItems.push({
group: sampleGroups[0],
title: json[i].text,
subtitle: "Item Subtitle: 1",
description: itemDescription,
content: itemContent,
backgroundImage: lightGray
});
}
return sampleItems;
}
我能看到sampleItems數據:
我明明做錯事,因爲當我將我的對象結果與原始示例數據的結果進行比較,結果幾乎相同。
任何幫助表示讚賞。
您的getData函數返回null或undefined。這就是錯誤信息告訴你的。您分享了錯誤功能的代碼。 –
getData調用OnSuccessCall - 爲了清晰起見,我將添加getData函數 – webdad3