就像标题说的那样,我正在为dojo/request/xhr使用新的 dojo 语法,但这似乎不起作用,并且在加载数据时抛出错误,而具有相同 url 的旧语法给出了想要的结果。
这是当前无法正确加载数据的语法:
this.get = function (url, loadFunc, errorFunc, handleFunc) {
xhr( url, {
handleAs: 'json'
}).then(function(data){
typeof loadFunc === 'function' ? loadFunc : function () { };
console.log(url+ " load");
console.log(data);
}, function(error){
typeof errorFunc === 'function' ? errorFunc : function () { };
console.log(url+" error");
console.dir(error);
}, function(handle){
typeof handleFunc === 'function' ? handleFunc : function () { };
console.log(url+" handle");
console.log(handle);
});
};
如您所见,我正在将数据打印到控制台,并且我得到了正确的数据,但是 xhr 请求引发了此错误:
“语法错误:在 l.json ( http://ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js:228:250 ) 在 m ( http://ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js:227:277 ) 在 j [as handleResponse] ( http://ajax.googleapis.com/ajax/ libs/dojo/1.8.1/dojo/dojo.js:151:351 ) 在 XMLHttpRequest.e ( http://ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js:154 :393 )"
虽然以下旧语法完美运行:
this.get = function (url, loadFunc, errorFunc, handleFunc) {
dojo.xhrGet({
url: url,
handleAs: 'json',
load: typeof loadFunc === 'function' ? loadFunc : function () { },
error: typeof errorFunc === 'function' ? errorFunc : function () { },
handle: typeof handleFunc === 'function' ? handleFunc : function () { }
});
};
编辑:
这是我的 JSON 数据:
{ "assistants" : [ { "assistants" : [ "M 11",
"M 1"
],
"name" : "M X1"
},
{ "assistants" : [ "M 2",
"M 2XX1",
"M 3"
],
"name" : "M 1"
},
{ "assistants" : [ "M 2" ],
"name" : "M 2"
},
{ "assistants" : [ ],
"name" : "M 3"
}
],
"chiefs" : [ { "chief" : "M X1",
"name" : "M 11"
},
{ "chief" : "M 11",
"name" : "M 1"
},
{ "chief" : "M X1",
"name" : "M 2"
},
{ "chief" : "M 744X1",
"name" : "M 3"
}
],
"departments" : [ { "assistants" : [ "M 11",
"M 3",
"M 21"
],
"chief" : "M X1",
"email" : "dg@somedomain.com",
"interim" : [ "M X542",
"M 4"
],
"members" : [ "M 2",
"M 3",
"M 4",
"M 5",
"M X24544"
],
"name" : "Dep1",
"notify" : [ "Dep2",
"M X2",
"M 21"
],
"resp" : "M 21",
"validators" : [ "Dep2",
"M 2",
"M 558"
]
},
{ "chief" : "M 1",
"email" : "admin@somedomain.com",
"members" : [ "M 11",
"M 12"
],
"name" : "Dep3",
"parent" : "Dep1"
},
{ "chief" : "M 11",
"email" : "commercial@somedomain.com",
"members" : [ "M 21",
"M 22"
],
"name" : "Dep4",
"parent" : "Dep1"
}
],
"orgaTestModel" : { "corporation" : "Corporation Sample",
"name" : "Orga Sample 7855"
},
"root" : "Dep1"
}
注意:
我正在使用dojo 1.8.1
版本,但我也对其进行了测试dojo 1.9.2
,但它仍然无法正常工作。
我不知道有什么问题。我的代码有问题还是另一个问题?
任何帮助,将不胜感激。