Jquery Ajax post animation during ajax process?

戏子无情 提交于 2019-12-23 04:34:12

问题


I do ajax post on my mvc app when dropdown change.

$(function () {
    $('#meters').change(function () {
        var analizor_id = $(this).val();
        if (analizor_id && analizor_id != '') {
            $.ajax({
                url: '@Url.Action("AnalizorInfoPartial", "Enerji")',
                type: 'GET',
                cache: false,
                data: { analizor_id: analizor_id },
                success: function (result) {
                    $('#TableAnalizorInfo').html(result);
                }
            });
        }
    });
});

DropDown

@Html.DropDownList("sno", new SelectList(Model, "sno", "AnalizorAdi"), "-- Analizör Seçiniz --", new { id = "meters" })

Can I show a loading image or anything else during ajax process? (between begin - finish event) and Code example?

EDIT

Can I use like this?

success: function (result) {
    $('#TableAnalizorInfo').html(result);
}
begin:function(){ 
    //show image
}
complete:function(){ 
    //hide image
}

Thanks.


回答1:


Of course, the events you are looking for are beforeSend and complete:

if (analizor_id && analizor_id != '') {
     $.ajax({
         url: '@Url.Action("AnalizorInfoPartial", "Enerji")',
         type: 'GET',
         cache: false,
         beforeSend: function() {
             // Show your spinner
         }, 
         complete: function() {
             // Hide your spinner
         },
         data: { analizor_id: analizor_id },
         success: function (result) {
            $('#TableAnalizorInfo').html(result);
        }
    });
}

or you could do it globally for all AJAX requests on the page using global AJAX event handlers:

$(document).ajaxSend(function() {
    // Show your spinner
}).ajaxComplete(function() {
    // Hide your spinner
});


来源:https://stackoverflow.com/questions/12108937/jquery-ajax-post-animation-during-ajax-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!