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 !