0

I have a strange situation with jquery ajax callback

var isOk = false;

 CheckStoreAvailability(store, function (result) {
            isOk = result;
             alert(result);  >> show message true    
});

alert(isOk );

it always show false no mater what return value in CheckStoreAvailability assigns to the isOk !!

i'm stucked now. Pls help me.

Thanks.

nam vo
  • 3,271
  • 12
  • 49
  • 76

2 Answers2

1

Your second alert is executed before the CheckStoreAvailability is called due to asynchronous call. That is why you are not getting the updated value.

Check the answer in this post how function call is used with done(), you may change your call accodingly.

Community
  • 1
  • 1
Adil
  • 146,340
  • 25
  • 209
  • 204
  • Check the answer in this post how function call is used with done, http://stackoverflow.com/questions/13648995/how-to-grab-return-value-from-an-ajax-call, you may change your call accodingly – Adil Dec 17 '12 at 15:28
1

AJAX is asynchronous, so that alert(isOk); line is going to be executed before the AJAX call has completed, and by extension before the value has been changed by the AJAX requests callback function.

If you want to work with that value, do it inside that function handling a response from the AJAX call.

Anthony Grist
  • 38,173
  • 8
  • 62
  • 76