0

I've added "async:false" on my ajax code, for a remote function, but it pushes everything that comes before it.

This is the code going on:

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;
}

Im calling server(), and the order should be: load();$.ajax();unload(); The acctual order is $.ajax();load();unload();

Please help :)

Thx, Amit.

Amit
  • 26
  • 8

1 Answers1

0

For loading the spinner , you dont need to call the function seperately, you can call that function inside ajax call itself by defining under beforeSend: to load spinner and to unload inside .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

This way of defining the ajax call will avoid the problem what your facing. One more thing when using any jquery function, check for all possible options to verify whether it suits your requirement. jQuery does support lot of functions, thats the reason it was created. Write Less, Do More

Happy Coding :)

dreamweiver
  • 6,002
  • 2
  • 24
  • 39
  • Thanks! I use AJAX and not GET not POST because i do a cross domain request. – Amit May 22 '14 at 14:27
  • @user3665372: Type can be get or Post , depending on your specific requirement.if its crossdomain that you want to achieve, then you many have to use `jsonp` instead of `json` as a dataType. **Ref:http://stackoverflow.com/questions/15477527/cross-domain-ajax-request** – dreamweiver May 22 '14 at 14:35