用异步加载jQuery Ajax

时间:2019-01-29 13:03:51

标签: javascript jquery ajax

jQuery Ajax加载不适用于异步

下面的代码

$('#pagination').on('click','a',function(e){
    e.preventDefault(); 
    var data = $(this).attr('data-pagination-page');
    var selector=$('.frmTable');        
    var response=getData(selector,data);    

});

Ajax函数

function getData(selector,data){
var result="";
var frmAction=$(selector).attr('action');   
$.ajax({
    url: frmAction+'/'+data,
    type: 'post',
    data: $(selector).serialize(),
    async: false,  
    beforeSend: function(){
        console.log("Request Submiting....");
        $('#loading').css('display','block');
    },
    success: function(response){
         result = response; 
    },
    complete:function(data){
        $('#loading').css('display','none');
        console.log("Request Complete....");
    }
});      
return result;
    }

能给我建议如何处理ajax加载图标。

1 个答案:

答案 0 :(得分:0)

使用 setTimeout 问题已解决

function getData(selector,data){
    var result="";
    var frmAction=$(selector).attr('action');       
    $.ajax({
        url: frmAction+'/'+data,
        type: 'post',
        data: $(selector).serialize(),
        async: false,  
        beforeSend: function(){
            console.log("Request Submiting....");
            $('#loading').css('display','block');           
        },
        success: function(response){
             result = response; 
        },
        complete:function(data){        
            console.log("Request Complete....");                
            setTimeout(function(){
                $('#loading').css('display','none');            
            }, 500);
        }
    });     
   return result;
}