如何使用dojo一个接一个地执行两个xhr.gets?
我有......
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(“Elapsed”)。innerHTML = elapsedContent ;
答案 0 :(得分:1)
在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
}
});
}
});
答案 1 :(得分:1)
虽然嵌套是一个简单的解决方案,但它几乎总会导致无法读取的代码,所以我会像@Ricardo那样做,但使用Dojo的Deferred(+ here)的优势并使用链接:
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;
})
在jsFiddle中查看它:http://jsfiddle.net/phusick/73X88/
答案 2 :(得分:0)
我认为您应该为 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
}
});