JavaScript: Global variables after Ajax requests [duplicate] JavaScript: Global variables after Ajax requests [duplicate] javascript javascript

JavaScript: Global variables after Ajax requests [duplicate]


What you expect is the synchronous (blocking) type request.

var it_works = false;jQuery.ajax({  type: "POST",  url: 'some_file.php',  success: function (data) {    it_works = true;  },   async: false // <- this turns it into synchronous});​// Execution is BLOCKED until request finishes.// it_works is availablealert(it_works);

Requests are asynchronous (non-blocking) by default which means that the browser won't wait for them to be completed in order to continue its work. That's why your alert got wrong result.

Now, with jQuery.ajax you can optionally set the request to be synchronous, which means that the script will only continue to run after the request is finished.


The RECOMMENDED way, however, is to refactor your code so that the data would be passed to a callback function as soon as the request is finished. This is preferred because blocking execution means blocking the UI which is unacceptable. Do it this way:

$.post("some_file.php", '', function(data) {    iDependOnMyParameter(data);});function iDependOnMyParameter(param) {    // You should do your work here that depends on the result of the request!    alert(param)}// All code here should be INDEPENDENT of the result of your AJAX request// ...

Asynchronous programming is slightly more complicated because the consequence of making a request is encapsulated in a function instead of following the request statement. But the realtime behavior that the user experiences can be significantly better because they will not see a sluggish server or sluggish network cause the browser to act as though it had crashed. Synchronous programming is disrespectful and should not be employed in applications which are used by people.

Douglas Crockford (YUI Blog)


AJAX stands for Asynchronous JavaScript and XML. Thus, the post to the server happens out-of-sync with the rest of the function. Try some code like this instead (it just breaks the shorthand $.post out into the longer $.ajax call and adds the async option).

var it_works = false;$.ajax({  type: 'POST',  async: false,  url: "some_file.php",  data: "",  success: function() {it_works = true;}});alert(it_works);

Hope this helps!


It seems that your problem is simply a concurrency issue. The post function takes a callback argument to tell you when the post has been finished. You cannot make the alert in global scope like this and expect that the post has already been finished. You have to move it to the callback function.