2013-01-21 134 views
0

我有多個ajax調用在頁面加載時觸發。同時ajax請求django

request1我希望response1,並從request2我期待response2。但我收到request1request2response1

當我移動request2response1已經收到火,然後我收到request2如預期。

如何安全地執行多個ajax調用?

一些代碼:

//avoids problem 
    this.update = function(){ 
     $.post('/ws/newsfeed/', {'action':'6'}).done(function(response){ 
      ... 
      _this.get_alert_count() 
     }) 
    } 

    this.get_alert_count = function(){ 
     $.post('/ws/newsfeed/', {'action':'2'}).done(function(count) { 
      ... 
     }) 
    } 

_this.update() 

//causes problem 
    this.update = function(){ 
     $.post('/ws/newsfeed/', {'action':'6'}).done(function(response){ 
      ... 
     }) 
    } 

    this.get_alert_count = function(){ 
     $.post('/ws/newsfeed/', {'action':'2'}).done(function(count) { 
      ... 
     }) 
    } 

_this.update() 
_this.get_alert_count() 
+3

如果你不介意,你可以發佈一些示例代碼?這可能會有所幫助! – Mutant

+0

您是否嘗試過運行Firebug來檢查請求和響應,以確保您的JS中沒有錯誤? – Spacedman

+0

@Spacedman使用鉻。我檢查過開發工具是。你可以看到上面的代碼有多簡單 - 沒有錯誤。 – rikAtee

回答

0

你可能想調查AjaxQ(http://code.google.com/p/jquery-ajaxq/),它可以讓你連鎖的Ajax調用,而不在給定的重疊時間。

希望它有幫助

+0

只是記住你的ajax請求的順序並不能保證各個響應的順序是一致的。這是ajaxq來的地方。 – elsadek