Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

janisozolins's avatar

Is there an alternative to Ajax call?

I am currently using the following Ajax call to retrieve information from the database dynamically based on the select value in a field. However, it is horribly slow and causes dropdown selects and datepicker inputs to lag...

var array = [];

  $('#a_date').datepicker({
     dateFormat: "yy-mm-dd",
     beforeShowDay: getAvailableDays
  });

function getAvailableDays (date) {
    var doctor_id = $('#a_doctor_id').val(); //TODO: SET DEFAULT DOCTOR TO NOTHING

        $.ajax({
          url: '/getDoctorsAppointments/'+doctor_id+'/',
          type: "GET",
          dataType: "json",
          async: false,
          success: function (data) {
              array = data;
           }
        }); 

    var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
    return [array.indexOf(string) >= 0 ]

Is there anything else that I can use to retrieve an array of data from the database to my JavaScript? Ajax is terribly slow this way when I use async: false option....

0 likes
4 replies
jekinney's avatar

Not really other then loading all the data via php and using jquery/JavaScript to filter said data.

Bigger issue though is why is it slow? As so many new sites are API/hybrid sites (Facebook, twitter, almost all mobile apps etc) that the front end is all ajax connecting to an API proves it's viable option.

Possible bigger issues that not using ajax won't solve. From my experience it usually is one of two things:

Horrible and inefficient query (grabbing everything, not just columns and rows needed), n+1 issue (not eager or lazy loading relationships)

Inadequate server with not enough power. (Shared hosting, low memory etc)

jlrdw's avatar

Yes an iframe. Also you can use a pop up look up table.

janisozolins's avatar

@jekinney I read about async: false causing this problem for many other people too. The query is fast (I'm using embedded models for this, using MongoDB and it's blazing fast) and the computing power seems to be enough too as it's currently on my local development server and the application itself is not that complex. I've heard people recommend using Ajax Promises but I'm lacking knowledge in JS to know how to use it.

jekinney's avatar

Look into axios. It's a promised based ajax plugin.

1 like

Please or to participate in this conversation.