How to make a jQuery ajax call

→ Are you a new visitor? Please visit the page guidance for new visitors ←

How to make a jQuery ajax call

I’ve had this exact thing a few days ago when I wanted to run form post requests in background asynchronous and wanted to have the form output after each iteration from looping through an array. I received from my friends that I could have used output_buffering off or flushing the output buffer at each iteration to get the output I wanted, however this is not the safest thing nor the best solution if you have a large array to loop. In such cases, it is recommended to use an jQuery ajax call for each of your array element, so that even if you have issues when one of the element from the array hangs or gives an error, this will not break your website, but rather will return an error on the jQuery ajax call individually and your website will run as normal.

So straight to the point, bellow its a simple example I used in one of my projects (well not exactly the same, I removed some to only show you the jQuery ajax call):

Simple example of jQuery ajax call

Now to explain a bit what is going on. I’m using the above ajax call to submit a post request to the file check_async.php. That file receives the form data that is being sent using the ajax call through a POST method. This will only trigger itself once the form submit button is being clicked.

Of course, there are shortcuts to this ajax call called jQuery.Post(), an example of the same call is bellow:

This is indeed faster, but not so much control over it.

Using the GET method is also the same, you just need to change type to POST:

or using the simple jQuery shortcut:

Why it is based to use ajax instead of a normal form post?

Request an article ←