2014-05-22 17 views
0

我在ajax代碼中添加了「async:false」,用​​於遠程函數,但它推動了它之前的所有內容。Ajax函數將所有東西放在一邊

這是代碼怎麼回事:

function load() { 
$("#Load").show(0,function(){ 
    console.log('Spinner Loaded'); 
}); 
} 
function unload() { 
    $("#Load").hide(); 
    console.log('Load Ended'); 
} 
function server(datasend) { 
    load(); 
    var response; 
    $.ajax({ 
     type: 'POST', 
     url: 'http://mailsnitch.ipx-il.com/get/index.php', 
     dataType: 'json', 
     timeout:4000, 
     async: false, 
     data: datasend, 
     success: function(valueable){ 
      console.log('Success'); 
      response = valueable; 
      console.log(valueable); 
     }, 
     error: function(jqXHR, exception) { 
      if (jqXHR.status === 0) { 
       alert('Not connect.\n Verify Network.'); 
      } else if (jqXHR.status == 404) { 
       alert('Requested page not found. [404]'); 
      } else if (jqXHR.status == 500) { 
       alert('Internal Server Error [500].'); 
      } else if (exception === 'parsererror') { 
       alert('Requested JSON parse failed.'); 
      } else if (exception === 'timeout') { 
       alert('Time out error.'); 
      } else if (exception === 'abort') { 
       alert('Ajax request aborted.'); 
      } else { 
       alert('Uncaught Error.\n' + jqXHR.responseText); 
      } 
     }, 
    }); 
    unload(); 
    return response; 
} 

林打電話server(),並且順序應該是:load();$.ajax();unload(); 的acctual順序是$.ajax();load();unload();

請幫助:)

THX,阿米特。

+0

如何要檢查順序? –

回答

0

對於加載微調器,你不需要單獨調用該函數,你可以通過在beforeSend:下定義加載微調器並在.done內部卸載來調用自己的函數。

的Jquery:

$.ajax({ 
    type: 'POST', 
    url: 'http://mailsnitch.ipx-il.com/get/index.php', 
    dataType: 'json', 
    timeout:4000, 
    async: false, 
    data: datasend, 
    beforeSend:function() { 
        $("#Load").show(0,function(){ 
         console.log('Spinner Loaded'); 
        }); 
       } 
    ... 
    }).done(function() { 
       $("#Load").hide(); 
       console.log('Load Ended'); 
      });//end of ajax call 

定義Ajax調用將避免什麼問題你面對的這種方式。還有一件事,當使用任何jQuery的功能,檢查所有可能的選項來驗證它是否符合您的要求。 jQuery確實支持很多功能,這就是它創建的原因。 少寫,多做

編碼快樂:)

+0

謝謝!我使用AJAX,而不是GET不POST,因爲我做了一個跨域請求。 – Amit

+0

@ user3665372:類型可以是get或Post,具體取決於您的具體需求。如果您想要實現其跨域,那麼您必須使用'jsonp'而不是'json'作爲數據類型。 **參考:HTTP://stackoverflow.com/questions/15477527/cross-domain-ajax-request** – dreamweiver

相關問題