2017-07-18 4 views
2

私はいくつかのajax呼び出しを行う関数を持っています。
すべての方法を実行するには、これらの要求をasync: falseで設定する必要があります。

すべてはajax呼び出しで問題ありません。私の問題は、要求を送信して後に非表示にする前に表示するdiv(シンプルなCSSローダー)が必要ですが、表示されていないことです。要求を行うdivを表示または非表示にしていない非同期false ajaxコール

$("#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; 
    }); 

マイdoSomeStuff()機能:

は、これは私の関数である

function doSomeStuff(callback){  
      //... 
      for (var i = 0; i < ids.length; i++) { 
       var Id = ids[i][0]; 
       var ch = ids[i][1]; 
       var tp = 2;        
       var url = 'http://domain.com.br:8080/datasnap/rest/TSM/Fun/' + tp + '/' + $("#select").val() + '/' + ch; 
       $.ajax({ 
        cache: "false", 
        async: false, //it needs to by with async false 
        dataType: 'json', 
        type: 'get', 
        url: url, 
        success: function(data) { 
         if (!data) 
          toastr.error('error'); 
        }, 
        error: function(jqXHR, textStatus, errorThrown) { 
         toastr.error("some problem"); 
        } 
       }); 
      } 
      callback({ok: true}); 
    } 

私はこれを扱うことができる方法上の任意のアイデア?私は非同期のもので本当に新しいです。

答えて

0

これを解決するには、asyncを削除し、サーバーのmthodをパラメータとして配列を受け取るように変更します。

最終スクリプト:要求を行う

$("#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());    
      } 
     }); 
    }); 

マイdoSomeStuff()関数:

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"; 
        } 
      });     
    } 
関連する問題