我认为beforeSend函数在chrome上运行不正确

时间:2017-08-21 08:42:37

标签: javascript ajax google-chrome loading

当我发送" async = false"请求使用ajax,是否可以在响应期间显示加载标志。是的,出现警报信息,但我从未见过加载标志。

顺便说一句,Firefox没有任何问题。实际上,当我将此异步值更改为true时,它也适用于chrome。

有什么想法吗?

var HttpUtil = {

get : function(url, onSuccess, onFailure) {
    this.ajaxCall('GET', url, this.defaultContentType, null, false, false, onSuccess, onFailure);
},

post : function(url, request, onSuccess, onFailure) {
    var showSuccessMsg = url.indexOf('/filter') < 0;
    this.ajaxCall('POST', url, null, JSON.stringify(request), showSuccessMsg, true, onSuccess, onFailure);
},

postFormData : function(url, request, onSuccess, onFailure) {
    this.ajaxCall('POST', url, false, request, true, true, onSuccess, onFailure);
},

put : function(url, request, onSuccess, onFailure) {
    this.ajaxCall('PUT', url, null, JSON.stringify(request), true, true, onSuccess, onFailure);
},

delete : function(url, onSuccess, onFailure) {
    this.ajaxCall('POST', url, null, null, true, true, onSuccess, onFailure);
},

ajaxCall : function(type, url, contentType, request, showSuccessMsg, showLoading, onSuccess, onFailure) { 
    if(contentType == null){
        contentType = 'application/json';
    }
    $.ajax({
        url: url,  
        type: type, 
        data: request,
        async: false,
        contentType : contentType,
        processData : false,
        success: function(data) {
            if(showSuccessMsg){
                ShowMessage.success("Success!", function onConfirm(){
                    onSuccess(data);
                });
            }else{
                onSuccess(data);
            }
        },
        error: function(error) {
            if (onFailure instanceof Function) {
                onFailure(error);
            } else {
                if(error.responseJSON){
                    ShowMessage.error(error.responseJSON.status, error.responseJSON.message);
                }else{
                    ShowMessage.error(JSON.stringify(error));   
                }
            }
        },
        beforeSend : function() {
            if(showLoading){
                //$('.loading1').css("z-index","11111");
                $('.loading1').css("display","block");
                //alert("1");
            }
        },
        complete : function() {
            if(showLoading){
                //$('.loading1').css("z-index","-1");
                $('.loading1').css("display","none");
            }
        }
    });
}

};

2 个答案:

答案 0 :(得分:0)

试试这个:

尝试在发送之前添加1 ms的延迟:

beforeSend : function() {
            if(showLoading){
                //$('.loading1').css("z-index","11111");
           setTimeout(function(){
             $('.loading1').css("display","block");
           }, 1);

                //alert("1");
            }
        },

答案 1 :(得分:0)

顺便说一下它的工作方式,但它的工作时间较晚:(

beforeSend : function() {
            if(showLoading){
                $('.loading').css("display","block");
            }
        },
        complete : function() {
            if(showLoading){
                setTimeout(function(){
                    $('.loading').css("display","none");
                }, 2000);
            }
        }