0

I have an ajax call inside ajax. Problem is I have to wait second ajax finish because until that time browser freezes. Why is this happening if I set it to async true? I dont want to wait for any response from this second inner ajax and I dont need any response from it. It is just an email notification to some users based and needs parameters from first ajax.

        $.ajax({
        url:     'route_process.php',
        cache:  false,
        async:  true,
        type:    'post',
        data:   {type: document.getElementById('type').value},
        dataType: 'html',
        success: function(data) {
            data = data.split("brk");
            $('.spinner').css({'display':'none'});
            $('#save_button').prop("disabled",false);
            $.ajax({
                url:     'sent_hike_drive_notification.php',
                cache:   false,
                type:    'post',
                async:    true,
                data:   {type: data[1], insert_id: data[2], date_search_array: data[3], from_city_lat: data[4], from_city_lng: data[5], to_city_lat: data[6], to_city_lng: data[7], counter: data[8], insert_id2: data[9], date_search_array2:data[10]},
                dataType: 'html',
                success: function(result) {
                }
            });
            Drive.resetRoute();
            alert(data[0]);

        },

Thanks all, now I found out the problem is not maybe in those ajaxs. User wants to move to another webpage through load when click on menu icon.

$('.main_body').find('.container').load(url);

This dont works until those ajax finish. So not complete browser freezes only I cannot navigate to next page.

Vilis
  • 9
  • 5
  • If you don't want any response from the outer ajax which will further use in the inner .ajx, then put the inner ajax independent and outside from the outer ajax. That is, call the inner ajax simultaniously and independently. Just make two functions, put 1st ajax in first one and 2nd is in second one. And call them separately. – Naveen Chandra Tiwari Sep 24 '14 at 11:56
  • browser must not freeze. I think your Drive.resetRoute() function is creating problem. My suggestion please comment your Drive.resetRoute() fn and try again. – maddy Sep 24 '14 at 11:58
  • @Harsh Kaushal I dont want any response from inner ajax. – Vilis Sep 24 '14 at 12:01
  • @Mayur Rahul I tried that and still not working well. – Vilis Sep 24 '14 at 12:01
  • did you try using closing the session which may be one of the cause: session_write_close(); – daksh_019 Sep 24 '14 at 12:52

3 Answers3

0

Common, Ajax is indeed async but You should remember that a success handler is called only when the ajax call is complete whether sync or async So this inner ajax call can be called only when the first is complete. There is no way you can access inner ajax call without first being completed. All you can do is make an ajax call via callback on the success handler of the outer ajax.

Ekansh Rastogi
  • 2,418
  • 2
  • 14
  • 23
  • thanks, I know but I have to wait until second ajax finish until go to next page on my url. Browser freezes for a several seconds until success on second ajax. I dont want to wait for second ajax finish. – Vilis Sep 24 '14 at 11:57
  • why would you in anyway send response to the client side and then again send ajax based on the same data, why not just do this processing at server side and send the final output, after this is also completed. – Ekansh Rastogi Sep 24 '14 at 12:11
  • becasue inner ajax take sometimes 20 seconds. I dont want user to wait so long time. If first ajax is just second. Based on data from first ajax second one is searching huge databases to whom should sent email notifications. – Vilis Sep 24 '14 at 12:14
  • I would still recommend that you do the thing in inner ajax at server side, it will at least reduce your network lag, well what you can do is show a loading message or something till then, – Ekansh Rastogi Sep 24 '14 at 12:16
  • Thanks but this is not solution for me. Everybody will angry to wait so long. – Vilis Sep 24 '14 at 12:26
0

This is not an exact answer to your problem but only a help/checklist :

The most common causes of the UI freezing on making an ajax call when the call is async:true are as follows:

  1. A lot of Dom manipulation taking place on response of the call.
  2. A lot of data is being sent by the server and processing it requires time.
  3. If a session is being maintained and it is not closed before multiple ajax callbacks (this happens to avoid race conditions).
  4. Something on the server side goes wrong/extremely slow on the ajax call and the client side is stuck.
  5. Network speed is too slow.

EDIT: Based on the response from asker , to help others if they face similar issue.

  1. Is there a redirection involved which is locking up with ajax response.

It is highly unlikely that the UI freezes despite all the check points mentioned above taken care of.

In any case the solution might be one of the following approaches :

  1. Try making a call to the second ajax after a timeout of like 5000 ms.
  2. Try promises : how does jquery's promise method really work?
  3. (worst way)Try using an iframe to make the requests.
Community
  • 1
  • 1
daksh_019
  • 802
  • 7
  • 16
  • Thanks now I found out the problem is not maybe in those ajaxs. User wants to move to another webpage through load. $('.main_body').find('.container').load(url); and this is freezed until those ajax finish. So not complete browser only I cannot navigate to next page. – Vilis Sep 24 '14 at 14:07
-1

try to use promise.

$.ajax({
    url:     'route_process.php',
    cache:  false,
    async:  true,
    type:    'post',
    data:   {type: document.getElementById('type').value},
    dataType: 'html'
    }).promise().then(function(data) {
        data = data.split("brk");
        $('.spinner').css({'display':'none'});
        $('#save_button').prop("disabled",false);
        $.ajax({
            url:     'sent_hike_drive_notification.php',
            cache:   false,
            type:    'post',
            async:    true,
            data:   {type: data[1], insert_id: data[2], date_search_array: data[3], from_city_lat: data[4], from_city_lng: data[5], to_city_lat: data[6], to_city_lng: data[7], counter: data[8], insert_id2: data[9], date_search_array2:data[10]},
            dataType: 'html',
            success: function(result) {
            }
        });
        Drive.resetRoute();
        alert(data[0]);

    })
Nodarii
  • 934
  • 7
  • 21