× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

How to Make Synchronous AJAX Calls in JQuery

David Brumbaugh
Aug 05, 2016
<p>Synchronous AJAX is probably an oxymoron. The <em>A</em> in <em>A</em>jax stands for "Asynchronous" .  The Asynchronous nature of AJAX allows the browser to exchange data with a server behind the scenes, while the user is doing other things. This is usually better than having them wait for an entire Web page to refresh.</p> <p>Most of the time, it's best to leave keep AJAX asynchronous. </p> <p>However, there are rare times, for example when you need to know the ID of a saved data item before you can continue processing, that it may be necessary to wait until the server has completed its work.</p> <p>In that case, you want may want to set the async parameter to false to assure that the server has completed its work before continuing.  Below is a short example of how you would do this.</p> <pre><code class="language-javascript">$.ajax({ type: 'POST', url: 'create_new_record.php', //Returns ID in body data: data_from_form, async: false, // &lt;&lt;== THAT makes us wait until the server is done. success: function(data){ populate_dom_with_id(data); // So we can use it }, error: function() { alert("Your error message goes here"); } }); </code></pre> <p> </p>
comments powered by Disqus