Ajax 使用dojo实现多个xhr.get
如何使用dojo一个接一个地执行两个xhr.get 我有Ajax 使用dojo实现多个xhr.get,ajax,dojo,Ajax,Dojo,如何使用dojo一个接一个地执行两个xhr.get 我有 require(["dojo/_base/xhr", "dojo/dom", "dojo/domReady!"], function(xhr, dom) { // Using xhr.get, as very little information is being sent xhr.get({ // The URL of the request url: "inc/etl2json.php
require(["dojo/_base/xhr", "dojo/dom", "dojo/domReady!"],
function(xhr, dom) {
// Using xhr.get, as very little information is being sent
xhr.get({
// The URL of the request
url: "inc/etl2json.php?item=Execs",
// The success callback with result from server
load: function(execContent) {
dom.byId("Execs").innerHTML = execContent;
},
// The error handler
error: function() {
// Do nothing -- keep old content there
}
});
});
我想做另一个xhr.get到“inc/etl2json.php?item=Execs”并将其分配给dom.byId(“已用”).innerHTML=elapsedContent 只需在load函数中再次调用xhr.get(),如果内容应该更改,则可以使用第一次检索到的相同数据:
xhr.get({
load:function(data){
//use the first data you retrieved
xhr.get({
load: function(data2){
//do what you like with the nuew data
}
});
}
});
我认为您应该为elapsedContent添加另一个
xhr
调用。我看不出这两个电话之间有什么关系,所以你应该把它们分开。相互嵌套是没有必要的。
加上
xhr.get({
// The URL of the request
url: "inc/etl2json.php?item=Execs",
// The success callback with result from server
load: function(elapsedContent) {
dom.byId("Elapsed").innerHTML = elapsedContent;
},
// The error handler
error: function() {
// Do nothing -- keep old content there
}
});
尽管嵌套是一个简单的解决方案,但它几乎总是导致无法读取的代码,所以我会像@Ricardo一样做,但利用Dojo(+)的优势并使用链接:
var requestUrl = "inc/etl2json.php?item=Execs";
xhr.get({ url: requestUrl})
.then(function(results) {
dom.byId("execs").innerHTML = results;
})
.then(function(results) {
return xhr.get({ url: requestUrl});
})
.then(function(results) {
dom.byId("elapsed").innerHTML = results;
})
在JSFIDLE上查看它的实际操作: