Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 显示或隐藏不使用异步错误ajax调用的div_Javascript_Jquery_Ajax_Show_Show Hide - Fatal编程技术网

Javascript 显示或隐藏不使用异步错误ajax调用的div

Javascript 显示或隐藏不使用异步错误ajax调用的div,javascript,jquery,ajax,show,show-hide,Javascript,Jquery,Ajax,Show,Show Hide,我有一些函数可以进行一些ajax调用。为了以所需的方式执行所有请求,这些请求必须通过设置async:falseajax调用一切正常。我的问题是,我需要在发送请求之前显示一个div(一个简单的css加载器),并在请求之后隐藏,但它没有显示。这是我的功能: $("#btn1").on('click', function(){ // Apparently it does not executes this $('.container-loader'

我有一些函数可以进行一些ajax调用。
为了以所需的方式执行所有请求,这些请求必须通过设置
async:false


ajax调用一切正常。我的问题是,我需要在发送请求之前显示一个div(一个简单的css加载器),并在请求之后隐藏,但它没有显示。

这是我的功能:

$("#btn1").on('click', function(){  
       // Apparently it does not executes this          
        $('.container-loader').show(); 
        //execute the function and handle the callback
        doSomeStuff(function(c){
            if(!c.ok){
                showModalWarning();
                $('#text').append('<li>'+c.msg+'</li>');
            }
            else{
                toastr.success('Everything is ok');
                doOtherStuff($("#select").val());               
            }
        });

        var modal = document.getElementById('Modal1');
        modal.style.display = "none";
        return false;
    });

你知道我该怎么处理吗?我对异步的东西很陌生。

通过删除异步并更改服务器中的方法以接收数组作为参数,解决了这个问题

最终脚本:

$("#btn1").on('click', function(){             
        //$('.container-loader').show(); 
        //execute the function and handle the callback
        doSomeStuff(function(c){
            if(!c.ok){
                showModalWarning();
                $('#text').append('<li>'+c.msg+'</li>');
            }
            else{
                toastr.success('Everything is ok');
                doOtherStuff($("#select").val());               
            }
        });
    });
$("#btn1").on('click', function(){             
        //$('.container-loader').show(); 
        //execute the function and handle the callback
        doSomeStuff(function(c){
            if(!c.ok){
                showModalWarning();
                $('#text').append('<li>'+c.msg+'</li>');
            }
            else{
                toastr.success('Everything is ok');
                doOtherStuff($("#select").val());               
            }
        });
    });
function doSomeStuff(callback){     
            //...
            var tp = 2;                             
            var url = 'http://domain.com.br:8080/datasnap/rest/TSM/Fun/' + tp + '/' + $("#select").val() + '/' + encodeURIComponent(JSON.stringify(jsonArray));
            $.ajax({
                    cache: "false",
                    //async: false, //it needs to by with async false
                    dataType: 'json',
                    type: 'get',
                    url: url,
                    success: function(data) {
                        if (!data)
                         callback({ok: false});
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        toastr.error("some problem");
                    }, complete: function(){ //hide the loader after complete
                        $('.container-loader').hide();
                        var modal = document.getElementById('Modal1');
                        modal.style.display = "none";
                    }
            });                
    }