简体   繁体   中英

Using multiple ajax calls in one javascript function

Thanks in advance for any help.

Is it bad practice and/or inefficient to use multiple $.ajax calls in one javascript function? I've been working on one and have a simple testing environment set up on my computer (apache server with php/mysql), and I've noticed that the server will crash (and restart) if I have multiple ajax calls.

I have two ajax calls currently: one passes 4 pieces of data to the php file and returns about 3 lines of code (pulling info from my sql database), the other simply gets the total rows from the table I'm working with and assigns that number to a javascript variable.

Is it just that my basic testing setup is too weak, or am I doing something wrong? See below for the two ajax calls I'm using:

$.ajax({
    type: "GET",
    url: "myURLhere.php",
    cache: false,
    data: {img : imageNumber, cap : imageNumber, width : dWidth, height : dHeight},
})
    .done(function(htmlpic) {
        $("#leftOne").html(htmlpic);
    });

$.ajax({
    type: "GET",
    url: "myotherURLhere.php",
    cache: false,
    success: function(data) {
        lastImage = data
    }
})

Short answer: two ajax request on a page is absolutely fine.

Longer answer:

You have to find the balance between minimalising the number of ajax calls to the backend reducing the traffic and communication overhead; and still maintaining a maintainable architecture (so do not pass dozens of parameters in one call to retrieve everything - maybe only if you do it in a well designed way to collect every parameter to send)

Also most likely there's something wrong with your backend setup, try looking into webserver logs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM