Asynchronous server responses for AJAX calls

Asked

Viewed 205 times

3

I have an application that at the end of a page loading I make an AJAX request that takes around 20 seconds.

After this request my page keeps working normally so that when I try to make another AJAX request it waits for the first return the result to be executed later.

You can execute the second request without having to receive the result of the first?

Frontend

$.ajax({
       url: '@Url.Action("NavegarNaEscala")',
       method: 'post',
       cache: false,
       data: { escala: escala, direcao: direcao },
       success: function (data) { 
       },
       error: function () {
           ExibirMensagem(1, "Ocorreu um erro ao navegar na escala.");
       }
  }); 

Backend

 public JsonResult NavegarNaEscala(string escala, string direcao)
 {...}
  • 2

    Put your code, @Igor Teixeira

  • By the browser developer tool (F12), in Network, you can identify asynchronous calls and be sure if one starts after the other or if one is expecting some resource from the other.

1 answer

1

I always use Jquery in my AJAX requests:

$(document).ready(function(){
    /*Primeira requisição*/
    $.ajax({...});

    /*Segunda requisição*/
    $.ajax({...});
})

In this case, none of the requests waits for the other to finish to start.

$(document).ready(function(){
    /*Primeira requisição*/
    $.ajax({
        success:function(data){
            /*Segunda requisição*/
            $.ajax({...});
        }
    });
})

In this case the second request will only start after the termination (successfully. No communication error with the server or bad formatting of the return file.) of the first.

If you are not doing something like this and even then the second only starts when the first one ends, then it may be a typical case of locking resources in the database.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.