msitworld msitworld - 1 year ago 117
Ajax Question

Some commands don't executed after ajax is completed or successed

In below code after ajax is completed or successed

$(".cdmodels").append(result);
does not executed but two next lines are executed. i have checked different way but it's not solved. please help me.

$('.load-more').on('click', function (e) {
e.preventDefault();
var count = $(this).attr('data-value');
var btn = $(this).button('loading');
$.ajax({
url: "@Url.Action(MVC.Library.Index())",
data: { count: count },
dataType: 'json',
async: true,
method: 'POST',
complete: function (result) {
$(".cdmodels").append(result);
btn.button('reset');
btn.attr("data-value", parseInt(count) + 1);

}
});
});


UPDATE:

browser debug:

enter image description here

Answer Source

Usually when you are working with AJAX calls you should check for the type of result that you are getting

$('.load-more').on('click', function (e) {
    e.preventDefault();
    var count = $(this).attr('data-value');
    var btn = $(this).button('loading');
    $.ajax({
        url: "@Url.Action(MVC.Library.Index())",
        data: { count: count },
        dataType: 'json',
        async: true,
        method: 'POST',
        complete: function (result) {
            // console.log(result);  view the result
            // Array.prototype.toString.call(result); check typeof result
            // in your case it should be result.responseText
            $(".cdmodels").append(result.responseText);
            btn.button('reset');
            btn.attr("data-value", parseInt(count) + 1);

        }
    });
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download