2012-04-12 24 views
0

我該如何做兩個xhr.gets一個接一個地使用dojo?多個xhr.get與dojo

我....

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?項目=高管」並將其分配給dom.byId(」已逝「)。innerHTML = elapsedContent;

回答

1

只是再次調用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 
      } 
     }); 
    } 
}); 
0

我想您應該添加另一個xhr呼叫elapsedContent。我沒有看到兩個電話之間有任何關係,所以你應該讓他們分開。不需要在另一箇中嵌套。 只需添加

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 
     } 
    }); 
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/