Tuesday, December 27, 2011

Way to wait for jQuery's JSON method retruning from server

There are some scenarios where we may need to set the value for the text box or some html control based up on the value returned from the JSON.

I had scenario where two method.

1) JSON call
2) Normal Jquery/javascript function call

My thought was that till JSON call got executed my second method wont execute, since JSON is ASYNC call it wont till it get response from JSON, it will start executing our Jquery method.

The problem starts here, i was setting few variable(global) in 1st method and using them in the 2nd method.

Since it takes time to execute the 1st method, so the values will be loaded properly.

So we need to use $(document).ready(function () {
$.ajaxSetup({'async': false});
//Method1()
//Method2()
});

This will work like synchronous and this fixed my issue.