I am making an AJAX request that updates the value of a variable (foo) with a response from the server. Here is the code I am using:
foo
The (first) A in "AJAX" stands for asynchronous. The transaction does not happen immediately, and so your alert() happens quite some time before the remote call completes.
alert()