使用ajax的基本jquery延迟使用

时间:2015-01-26 18:55:42

标签: javascript jquery ajax

我正在尝试从How to capture asynchronous ajax response into a variable?重写代码以使用jquery延迟函数。我开始时:

    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并将其发送到控制台。我无法弄清楚如何将这些碎片放在一起。有人可以告诉我。

3 个答案:

答案 0 :(得分:1)

这是有效的,因为$ .ajax返回它自己的承诺(比如动画),否定了创建自己的延迟对象的需要。

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

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

答案 1 :(得分:1)

你在这里不需要一个完整的Deferred。您只需要一个Promise,其界面是Deferred的一个子集(有关详情,请参阅Deferred上的the doc)。 promise有一个方法.done(),允许您提供在异步进程成功结束时执行的回调。

$.ajax()方法返回一个方便实现Promise接口的jqXHR对象:

  

jQuery 1.5中$ .ajax()返回的jqXHR对象实现了   Promise接口,为它们提供所有属性,方法和   承诺的行为

所以当你致电$.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);
    }
});

答案 2 :(得分: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/