AJAX execution

1

Gentlemen,

I am in a question. I'm working with ASP.NET MVC and I'm running Ajax via post . The requirement is to load two grids with a Ajax for each grid. Strictly speaking, it would be something like that, Example:

Controller:

Public ActionResult grilla1()
{
return JSon(Datajson.cargagrilla1());
}

public ActionResult grilla2()
{ 
return JSon(Datajson.cargagrilla2());
}

$(document).ready(function(){
$.ajax({
        url: "@(Url.Action("grilla1", "CargaGrillas"))",
        type: "POST",
    dataType: "json",
    //data: ({ "rut": firstName }),
    success: function (Result) {
        Grilla1(Result);
    }});
$.ajax({
        url: "@(Url.Action("grilla2", "CargaGrillas"))",
        type: "POST",
    dataType: "json",
    //data: ({ "rut": firstName }),
    success: function (Result) {
        Grilla1(Result);
    }});
});

NOTE: They are examples nothing else, it is not necessary to analyze the code only its flow of execution:).

PS: The query is, since there are two independent ajaxes, the route of its execution will be simultaneous or the second ajax will have to wait for the first ajax to run and return its data and when the first ajax finishes its execution it will Could you run the second Ajax ??

I remain attentive to your comments, I hope you have understood me.

    
asked by Manu Keteimporta 13.11.2017 в 16:23
source

1 answer

1

Your code will make the first AJAX request, perform the second AJAX request, and then the main execution thread will end. When the server answers the calls, the functions declared in success will be called in the order in which the answers arrive: it is possible that the call of the "grid2" will be executed first or not, that will depend on the network and the server, nothing guarantees the order (that's why they are called asynchronous calls).

Small demonstration with similar behavior without using AJAX:

function ajaxMock(url,callback) {
  console.log('simulando llamada a '+url);
  setTimeout(() => {
    callback('Esto es una respuesta');
  },Math.random()*100);
}

console.log('Hago llamada 1');
ajaxMock('http://ejemplo', function (respuesta) {
  console.log('La respuesta a la primera llamada es ' + respuesta);
});

console.log('Hago llamada 2');
ajaxMock('http://ejemplo2', function (respuesta) {
  console.log('La respuesta a la segunda llamada es ' + respuesta);
});

If you execute it several times you will see that the order of the answer varies and, therefore, the order of the function called also.

    
answered by 13.11.2017 / 16:57
source