Ajax功能将一切都推到了一边

时间:2014-05-22 13:53:52

标签: jquery ajax loading

我已添加" 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(); 实际订单为$.ajax();load();unload();

请帮助:)

Thx,Amit。

1 个答案:

答案 0 :(得分:0)

为了加载微调器,你不需要单独调用该函数,你可以通过在beforeSend:下定义加载微调器并在.done内卸载来调用ajax调用本身内的函数。

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确实支持很多函数,这就是它创建的原因。 少写,多做

快乐编码:)