简体   繁体   中英

jquery ajax .done function not working

I have the following code which I am useing to dynamically load html into a codeigniter view:

               $.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);
                        },
                        done: function(){
                                console.log(' here is the html ', html);
                        }
                    });

I can see that the html is being returned correctly in firebug. However the 'done' function is not executing. What am I doing wrong?

You should you .done like this

$.ajax({
    type: "POST",
    url: "Ajax/getHtml",
    data: {
        u: contents
    },
    dataType: 'html',
    error: function(jqXHR, textStatus, errorThrown) {
        console.log('error');
        console.log(jqXHR, textStatus, errorThrown);
    }
}).done(function(html) {
   console.log(' here is the html ' + html);
});

It's usually a design error to have both a success handler and a .done promise handler, and in particular the latter doesn't belong inside the $.ajax call - it gets chained from the return value of the AJAX call:

The modern pattern is:

$.ajax({
    type: "POST",
    url: "Ajax/getHtml",
    data: {
        u: contents
    },
    dataType: 'html'
}).done(function(html) {   // nb: data is passed here
   console.log('here is the html:' + html);
}).fail(function(jqXHR, textStatus, errorThrown) {
   //...
});
    $.ajax({
       url: "test.html",
       context: document.body
    }).done(function() {
    $( this ).addClass( "done" );
    });

You have missing the "."done

http://api.jquery.com/jquery.ajax/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM