Michael makes a good point on why your implementation isn't working. Since you are making an asynchronous call, the order in which the javascript is executed is:
- Create Variable x
- Send Ajax call
- Execute console.log(x)
- Assign data to x (when then data is returned back)
The script will not wait for the data to return before executing the next line, as a result when the next line is called x has not been assigned to data.
If you need to handle the data in some way, you should create a function and call that function within the callback:
var x;
function handleData() {
console.log(x);
}
CartolaRutAccountAJAXFacade.getCargarValores(dwr.util.byId('combo').value, function(data){
x = data;
handleData();
});
Alternatively, you could pass the data into the function via a parameter if you need, instead of assigning it to x or using an anonymous function.
function handleData(data) {
console.log(data);
}
CartolaRutAccountAJAXFacade.getCargarValores(dwr.util.byId('combo').value, handleData(data));
Answer to Comments below
You won't be able to return x on the first on your first execution because it needs time for the Ajax call to return back data. If you are simply trying to return the data from within the entire function it may be easier to simplify everything to this:
useLoadingMessage();
dwr.engine.beginBatch();
CartolaRutAccountAJAXFacade.getCargarValores(dwr.util.byId('combo').value, function(data){
console.log(data);
// You may also want to do this, if you are looking
// to return the data from within evaluate_script:
//
// return data;
});
dwr.engine.endBatch();