0

I have an ajax call (post, async) executed on the opening event of a modal. I have a click event on a button inside that modal that launch an ajax call (post, async). When I click my button, its ajax call won't be done until the first one is done. How can I make both calls to run simultaneously?

Here is a sample of code:

$.ajax({ // On modal opening
    type: "POST",
    data: myData,
    success: function(response) {
        /*some code*/
    },
    async: true,
    url: url
});

$(document).on('click', '#myButton', function(e){
    $.ajax({
        type: "POST",
        data: myOtherData,
        success: function(response) {
            /*some code*/
        },
        async: true,
        url: url
    });
});
VeZoul
  • 500
  • 6
  • 19
  • why do you think they won't run in parallel? – Alnitak Jun 05 '17 at 14:09
  • Actually, they don't run in parallel. The second one is a simple post while the first one make a webservice call. The first takes 20+seconds and then, the second one runs in 1sec... – VeZoul Jun 05 '17 at 14:10
  • But they are running in sequence...the first only takes longer to finish execution. – apires Jun 05 '17 at 14:20
  • So I have no way to get the result of the second call while the first one did not ended? – VeZoul Jun 05 '17 at 14:21
  • unless you have other transfers going on such that you hit the limit on per-host simultaneous transfers they *should* be capable of running in parallel – Alnitak Jun 05 '17 at 14:26
  • 2
    Are they both contacting the same backend on the same server? If so what is that backend written in? The reason I ask is that you may be up against something like PHP's session locking. Even though the requests are running asynchronously and concurrently that does not mean that the backend server can serve them that way. – gforce301 Jun 05 '17 at 14:28
  • The backend is a PHP framework (CodeIgniter). Any way to check that? And yes, both request are for the same server/backend – VeZoul Jun 05 '17 at 14:30
  • 1
    You can start by searching "php session locking" and you can read this SO post, it may help: https://stackoverflow.com/questions/3371474/php-sessions-is-there-any-way-to-disable-php-session-locking – gforce301 Jun 05 '17 at 14:44

0 Answers0