View Single Post
Old 09-01-2010, 08:21 PM   PM User | #8
Shafayet_Ahmad
New to the CF scene

 
Join Date: Sep 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Shafayet_Ahmad is an unknown quantity at this point
I just found the solution to it.
May be, the original person who asked the question doesn't need it anymore, but future google searchers will have the solution when they need it.

Just put an extra option of "async: false" in the options hash. What's actually happening is that, since Ajax is "Asynchronous" Java Script and XML, it doesn't wait for the actual json value to get stored, but rather it returns immediately ( possibly with a null value ). So by the time the "success" routine gets called your mother function has moved on to the next line of execution ( and "success" is possibly running in a different thread ). To unset this asynchronous behaviour and make it wait just specify async:false

Code:
		   var myVariable = $.ajax({
		   	   
			   url: '/path/to/url',
			   dataType: 'json',
			   beforeSend: function(xhrObj){
	                xhrObj.setRequestHeader("Accept","application/json");
	           },
			   async: false,
			   success: function(data){
                               // do whatever
			   }
		   });
Hope this helps the next person.
Cheers !
Shafayet_Ahmad is offline   Reply With Quote