我正在为 Windows 8 开发我的第一个 WinJS 应用程序,我只是想替换 2 网格模板中给出的基本代码,我收到了这个错误:
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) { }
);
}
以下是从返回 JSON 数据的 php webservice 获取数据:
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 中看到数据:
我显然做错了什么,因为当我将我的对象结果与原始示例数据的结果进行比较时,它们实际上是相同的。
任何帮助表示赞赏。