{"id":1688,"date":"2010-09-14T21:06:27","date_gmt":"2010-09-14T15:36:27","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1688"},"modified":"2016-12-19T15:04:56","modified_gmt":"2016-12-19T09:34:56","slug":"asynchronous-behavior-of-ajax","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/asynchronous-behavior-of-ajax\/","title":{"rendered":"Asynchronous behavior of AJAX"},"content":{"rendered":"<p>Ajax (shorthand for Asynchronous JavaScript and XML) is used to retrieve data from the server asynchronously without interfering with the display and behavior of the existing page. Forgetting this asynchronous behavior will produce incorrect result if it depends on the response from Ajax call.<\/p>\n<p>Lets take an example(I am using <a href=\"http:\/\/api.jquery.com\/category\/ajax\/\" target=\"_blank\">JQuery<\/a> to illustrate the example). Here is a JavaScript function to perform server side check of the form. Depending on the response of Ajax call the function either submits the form or does nothing.<\/p>\n<div class=\"code\">\n<pre lang=\"javascript\">function submitForm(){\r\n\tvar isFormValid=false;\r\n\tvar dataToBeSent=$('form').serialize();\r\n\t$.get(url, dataToBeSent, function(result){\r\n\t\tisFormValid=result;\r\n\t})\r\n\tif(isFormValid){\r\n\t\t$('form').submit();\r\n\t}\r\n}<\/pre>\n<\/div>\n<p>The statement <code> $('form').submit()<\/code> will never be called irrespective of the <code>result<\/code> (AJAX call response). After executing <code>$.get<\/code> statement it will execute the next statement\u00a0 <code>if(isFormValid) <\/code>without waiting for the statement <code>isFormValid=result<\/code> inside the callback function to be executed.<\/p>\n<p>There exist a dirty solution for the problem : using JavaScript sleep API. But how long you are going to halt the execution of JavaScript code. You are back to the same problem of getting the incorrect behavior.<\/p>\n<p>One good solution is to make the $.get call synchronous by making changes to the global AJAX configuration like :<\/p>\n<div class=\"code\">\n<pre lang=\"javascript\">$.ajaxSettings.async=false;<\/pre>\n<\/div>\n<p>or using calls like :<\/p>\n<div class=\"code\">\n<pre lang=\"javascript\">$.ajax({type: \"GET\", url: url, data: dataToBeSent, success: function(){\r\n\t\tisFormValid=result;\r\n\t}\r\n});<\/pre>\n<\/div>\n<p>Now the<code> if(isFormValid) <\/code>statement will be executed only after the<code> $.ajax <\/code>statement has completed its execution. However this approach has its own drawback. Obvious one is the execution of JavaScript halts until it receives the response.<\/p>\n<p>However the better solution\u00a0 is to execute<code> $('form').submit()<\/code> inside the callback function like :<\/p>\n<div class=\"code\">\n<pre lang=\"javascript\">$('submitButton').click(function(){\r\n\tvar isFormValid=false;\r\n\tvar dataToBeSent=$('form').serialize();\r\n\t$.get(url, dataToBeSent, function(result){\r\n\t\tisFormValid=result;\r\n\tif(isFormValid){\r\n\t\t$('form').submit();\r\n\t}\r\n\t})\r\nreturn false;  \/\/ Needless if the button is not a submit button\r\n})<\/pre>\n<\/div>\n<p><code>return false<\/code> statement at the end of the function prevents the form from being submitted. Now the form submission code will be executed only after the response to<code> $.get<\/code> statement is true.<\/p>\n<p>There exist JQuery plugins (e.g. JQuery validation plugin) for doing form validation in a better way where you get support for server side validation also. But the case discussed above was to make you understand the asynchronous behavior of AJAX calls.<\/p>\n<p>Hope this helps you while using Ajax.\u00a0 \ud83d\ude42<\/p>\n<p>Cheers,<br \/>\n~~Bhagwat Kumar~~<br \/>\nbhagwat(at)intelligrape(dot)com<\/p>\n<p>http:\/\/www.tothenew.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ajax (shorthand for Asynchronous JavaScript and XML) is used to retrieve data from the server asynchronously without interfering with the display and behavior of the existing page. Forgetting this asynchronous behavior will produce incorrect result if it depends on the response from Ajax call. Lets take an example(I am using JQuery to illustrate the example). [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1],"tags":[96,410,408,27,409],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1688"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1688"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1688\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}