Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
running two ajax same time, one ajax call is using done

I was reading stackoverlow and found that there many questions related to this tag, i want to run two piece of codes simultaneously

using the $when

my code looks like this, but not sure how can i rewrite it, please guide

$(document).ready(function() {
    $(document).on('click', '#Button',function (e) {
        
        e.preventDefault();
        var isExcel = document.getElementById('Excel').checked ? 1 : 0;
        $.ajax({
            url: "page1",
            cache: false,
            data : $('#form').serialize(),
            method: "post",
            success: function(response){
                $('.mytable').show();
                $('.mytable').html(response);
            }
        }).done(function(data) {
            
            $("#datatables").DataTable({
                "bFilter": true,
                "serverSide": true,
                "deferRender":true,
                "processing": true,
                "ajax": {
                    "url" : "/data.cfm",
                    "type" : 'post'
                    }
                }
            });
        });
        
        if(isExcel) {
            $.ajax({ // 2nd call
                url: "/excel.cfm",
                cache: false,
                data : $('#form').serialize(),
                method: "post",
                success: function(response){
                    alert('done');
                }
            });
        }
    });
});

please guide how can i make it work using when, because the ajax call with datatables will bring the results back, the excel might take a while

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I want that when i click the hit button, it goes to the ajax call first which brings the datatables and start showing it. If the excel checkbox is checked, it calls another jquery ajax which generates the file from a different code in excel, but the datatables should be visible and in running state.

If you want the Excel part to run after the datatables returns, you should add the Excel request in the done callback of the datables call:

$(document).ready(function() {
    $(document).on('click', '#Button',function (e) {
        
        e.preventDefault();
        var isExcel = document.getElementById('Excel').checked ? 1 : 0;
        $.ajax({
            url: "page1",
            cache: false,
            data : $('#form').serialize(),
            method: "post",
            success: function(response){
                $('.mytable').show();
                $('.mytable').html(response);
            }
        }).done(function(data) {
            
            $("#datatables").DataTable({
                "bFilter": true,
                "serverSide": true,
                "deferRender":true,
                "processing": true,
                "ajax": {
                    "url" : "/data.cfm",
                    "type" : 'post'
                    }
                }
            });

            if(isExcel) {                                 // moved here
                $.ajax({ // 2nd call
                    url: "/excel.cfm",
                    cache: false,
                    data : $('#form').serialize(),
                    method: "post",
                    success: function(response){
                        alert('done');
                    }
                });
            }

        });

        // removed excel request from here
    });
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!