2015-01-26 75 views
0

我想重寫代碼從How to capture asynchronous ajax response into a variable?使用jQuery的延期功能。我開始:基本jQuery延遲使用ajax

var html =""; 

    $.ajax({ 
         type:"POST", 
         url: "Ajax/getHtml", 
         data: { u : contents }, 
         dataType: 'html',  
         success: function(data) { 
          html = data; 
         }, 

         error: function(jqXHR, textStatus, errorThrown) { 
           console.log('error'); 
           console.log(jqXHR,textStatus, errorThrown); 
         } 
        }); 

        console.log('html', html); 

我試圖把它變成一個延遲功能,可以返回所請求的HTML頁面時得到解決。我一直在閱讀http://learn.jquery.com/code-organization/deferreds/examples/http://jqfundamentals.com/chapter/ajax-deferreds這樣做。到目前爲止,我想出了:

    var html_response = new $.Deferred(url){ 

        $.ajax({ 
          type:"POST", 
          url: "Ajax/getHtml", 
          data: { u : url}, 
          dataType: 'html',  
          success: html_response.resolve, 
          error: html_response.reject 
         }); 

        }; 

這將用作部分:

    html_response().done{ 

         console.log('html', html); 

        } 

我所試圖做的是當get_html功能HTML返回成功地(即get_html解決)抓住html並將其發送到控制檯。我很難弄清楚如何把這些碎片放在一起。請有人給我建議。

+0

將ajax放入函數中並返回$ .ajax()對象 – Neve12ende12 2015-01-26 18:58:42

+2

'$ .ajax'自身返回一個承諾。無需創建自己的延期。 – atomman 2015-01-26 18:59:57

回答

1

,你在做什麼,有正確的概念。 如果我理解正確,你正在嘗試做一些「真正的異步」ajax,不像你上一篇文章。

你做錯了一件事是解決你的延期jQuery變量。

正確的代碼應該是這樣的:

  var html_response = $.Deferred(); // no need for new 

      function get_html(url){ 

       $.ajax({ 
         type:"POST", 
         url: "Ajax/getHtml", 
         data: { u : url}, 
         dataType: 'html',  
         success:function(data) { 
          html_response.resolve(data); //resolve is a fn 
         }, 
         error: function(x, status, err) { 
          html_response.reject(err); //reject is a fn 
         } 
        }); 

       }; 
      } 

      get_html(inserturlhere); 

       html_response.done(function(html){  // handle success 
        console.log('html: ' + html); 
       }) 
       .fail(function(error) {     // handle failure 
        console.log(error); 
       }); 

然而,AJAX有延遲已經自帶明確,所以一定要首先檢查了這一點。 http://api.jquery.com/jquery.ajax/

+0

謝謝,這正是我想要做的!我對ajax遲遲不會有點困惑。我讀過你發佈的文件。我假設你的意思是「你必須使用成功/錯誤/完整回調選項,而不是jqXHR對象的相應方法,如jqXHR.done()」? – user61629 2015-01-27 15:39:33

+0

我的榮幸! [這傢伙](http://www.htmlgoodies.com/beyond/javascript/making-promises-with-jquery-deferred.html)將完全分解。 – leedotpang 2015-01-30 14:46:14

1

這是可行的,因爲$ .ajax返回它自己的承諾(如動畫),否定創建自己的延遲對象的需要。

function html_response(url){ 
    return $.ajax({ 
    //ajax stuff 
    }); 
} 

html_response(url).done(function(data){ 
    console.log(data); 
}); 
+1

雖然它確實有效,但一點解釋會很好。 – 2015-01-26 19:01:31

+0

非常感謝! – user61629 2015-01-27 15:35:21

1

這裏您不需要全部Deferred。您只需要一個Promise,其界面是Deferred的子集(有關更多信息,請參見Deferred上的the doc)。承諾有一個方法.done(),它允許您提供一個回調,以便在異步過程成功結束時執行。

$.ajax()方法返回方便實現Promise接口jqXHR對象:

的jqXHR對象由$就()返回的jQuery 1.5的實施 無極接口,給他們的所有屬性,方法和 行爲的無極

所以,當你撥打$.ajax,你已經有了一個承諾。只要做到:

$.ajax({ 
    ... 
}).done(function(data){ 
    console.log(data); 
}); 

或者,如果你真的想處理一個完整Deferred對象,你可以這樣做:

var defr = $.Deferred(); 

defr.done(function(data){ 
    console.log(data); 
}); 

$.ajax({ 
    ... 
    ,sucCess : function(data){ 
     defr.resolve(data); 
    } 
}); 
+0

感謝您的幫助 – user61629 2015-01-27 15:35:00