0

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
    }
})
3
  • usually using multiple ajax in one javascript has no problem. you shold look for server log to see what causes the crash. Commented Apr 17, 2014 at 0:15
  • There must be something wrong with your setup. What is your server saying when you make such requests? Commented Apr 17, 2014 at 0:15
  • this is not wrong, unless you are planning to build something very optimized, although with one ajax request you can get both informations thus reducing client, network and server load. Commented Apr 17, 2014 at 0:24

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the info everyone. I'll try to figure out the backend setup- and thanks for the tips on optimizing the design of ajax/php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.