This is just the sequence of asynchronous operations. You don't update your variable in your function
BindNewSalesAttribute
; you do it in the function
success
. When your function
BindNewSalesAttribute
returns, the function object
success
is created, but not yet called, so the variable
varBindNewSalesTranGrid
is not yet updated. It will be updated when HTTP request is sent to the server side, the server side process it and sends HTTP response, which is handled on your client side by calling the function
success
of your
$.ajax
object.
To check it up, just call your alert in your
success
function, but also do some visually noticeable in your
error
function, in case the HTTP request is unsuccessful.
By the way, here you can observe the effect of
closure. Please see:
http://en.wikipedia.org/wiki/Closure_%28computer_programming%29[
^],
http://en.wikipedia.org/wiki/JavaScript#Functional[
^].
What to do? Redesign your code to avoid using an assumption of the order of calls of the functions in question. Avoid using outer-scope variables, which are best avoided. You need to abstract out your success/error actions from your library function
BindNewSalesAttribute
. Say, you can do it by adding a couple of function parameters (along with some other parameters).
—SA