Skip to content Skip to sidebar Skip to footer

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', u

Solution 1:

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

Solution 2:

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 hereconsole.log('here is the html:' + html);
}).fail(function(jqXHR, textStatus, errorThrown) {
   //...
});

Post a Comment for "Jquery Ajax .done Function Not Working"