我目前有一个对象数组,我希望使用xhrget调用我的HTML页面。这是我服务器中所述对象数组的片段:
[
{ "Name":"John" , "id":"1" },
{ "Name":"Anna" , "id":"2" },
{ "Name":"Peter" , "id": "3" }
{ "Name":"Peter" , "id": "4" }
]
然后,我使用xhrget
调用此对象数组require(["dojo/request", "dojo/dom", "dojo/dom-construct", "dojo/json", "dojo/on", "dojo/domReady!", "dojo/_base/xhr"],
function (dom,json,request,domconst,on,xhr){
dojo.ready(function(){
var targetNode = dojo.byId("licenseContainer");
var xhrArgs = {
url: "http://141.41.11.71:3000/page.json",
handleAs: 'json',
timeout : 2000,
load: function(data){
targetNode.innerHTML = data;
},
error: function(error){
targetNode.innerHTML = "An unexpected error occurred: " + error;
}
}
var deferred = dojo.xhrGet(xhrArgs);
});
});
我基本上希望将指定的对象放入我已由ID指定的node / div中。但是我一直收到错误:TypeError:'undefined'不是函数。
有什么建议吗?
答案 0 :(得分:1)
您可以访问值为john的mane属性data[0].Name
var xhrArgs = {
url: "http://141.41.11.71:3000/page.json",
handleAs: 'json',
timeout : 2000,
load: function(data){
targetNode.innerHTML = data[0].Name;
},
error: function(error){
targetNode.innerHTML = "An unexpected error occurred: " + error;
}
}
修改
显示你必须在数组中循环的所有名称。
load: function(data){
var html = '';
for (var i = 0; i < data.length; i++)
html += data[i].Name+'<br>';
targetNode.innerHTML = html;
},