Ok, this may be a dumb question, but I can’t find a way to solve my problem. I have this function:
function getActivityObj(sysId, date) {
var activityObj = dojo.xhrGet({
url: 'calendar/display-subactivities',
content: {'sysId': sysId, 'date': date},
handleAs: 'json',
load: function(result) {
console.log(result);
},
error: function(){
alert("error");
}
});
var ajaxResponse = activityObj.ioArgs.xhr.response;
return ajaxResponse;
}
The problem is that my ajaxResponse variable is always empty, but if in firebug, the xhr object the response property is not empty.
I will use the ajax response in several places in my code so what am I doing wrong or what would be a better way to call the ajax response? Thank you. (Sorry for my bad english)
I suspect the
ajaxResponsevariable is empty when you call it because thexhrGet()call is made asynchronously, and the result is not actually available yet when you setajaxResponseand return it from your function.By the time you view it in firebug, the XHR response has completed but it just isn’t there when your code executes.
xhrGet()returns adojo.Deferredobject. You can use that to add a callback function for when it actually completes:I am not certain if there’s a good way to wrap the
xhrGet()call in a function and attempt to return the response though, since it will always be deferred if called asynchronously.If it is safe to block further execution until the
xhrGet()call has actually returned data, you can call it synchronously. Then your code as you have it will work without a deferred callback. Instead you would typically do whatever work the returned data was intended for in thexhrGet()‘sload()function.