Login Register

Working with the result of a xhrGet call

Hi,

I don't understand how I can access the response from an xhrGet call using the defered object, I have tried various things, but nothing seems to work. I am a dojo newbie.

...
dojo.addOnLoad( function() {
	data = loader.loadData();
	consolo.log("data" + data");
	consolo.log("data" + data.results");
	consolo.log("data" + data.results[0]");
}
...

Imagine my Loader object has the following method:

...
loadData: function() {

	console.debug("Loader.loadData() start");

	var content = {"action":"ModelWorker", "mode":"get", "key":"top", "library":"dummy", "path":"/"};
	var result;

	var deferred = dojo.xhrGet( {
		// The following URL must match that used to test the server.
		url: this.serviceUrl,
		content: content,
		handleAs: "json-comment-filtered",
		timeout: 5000, // Time in milliseconds
		preventCache: true, // Prevent IE from caching this Get call

		// The LOAD function will be called on a successful response.
		load: function(response, ioArgs) {
			console.log("Loader.loadData() load, response: ", response);
			result = response;
			return response;
		},

		// The ERROR function will be called in an error case.
		error: function(response, ioArgs) {
			console.error("Loader.loadData() error, ", ioArgs.xhr.status);
			return null;
		}
	} );

	console.debug("Loader.loadData() result" + result);	
	console.debug("Loader.loadData() deferred" + deferred);
	console.debug("Loader.loadData() deferred results" + deferred.results);
	console.debug("Loader.loadData() end");

	return deferred;

},
...

... right, so when the loadData function is called, the url is called correctly and the consolo.log called on sucess is printing the correct results, however when the load function returns the result and my loadData returns the defered object suddenly I cannot access my result any more.

Not sure how to do this correctly. All the examples I could find all does some dojo.byId.innerHTML, which is not what I want.

Any ideas?

Kind Regards
Christian

the load: function doesn't

the load: function doesn't return to anywhere (nor error:) ...

to get the data from within there to a different scope, you might have to add the callbacks to the deferred after returning it. you can't put load: or handle: or error: methods in there, either.

try something like this:

loader:function(){

    var dfd = dojo.xhrGet({ url:foo /* other stuff, no load: */});
    return dfd
},
handler:function(data){
    // data should be the response data from the xhrGet call, whenever it happens to arrive.

},
starter:function(){
  var deferred = this.loader();
  deferred.addCallback(dojo.hitch(this,"handler"));
  deferred.addErrorback(function(err){
      console.log("an error.");
  });

}

Can someone please post a working example of this maybe?

Hi i'm noob at dojo..
Can someone please post a working example of this maybe?
I'm having nightmares get a similar concept to work!!

I want to poll a json resource for data regularly, then use that response data in a comparison statement...

---

var timer; //global var to keep track of the timer
var updateSettings = function(){

// do XmlHttpRequest
var deferred = dojo.xhrGet({
preventCache: true,
url : "./settings.json",
handleAs : "json",
sync : false, // explicit asynchronous request
timeout: 4000,

// called when the data loaded successfully
load: function(response,ioArgs){
source = response.settings[0].source;
format = response.settings[1].format;

// i get some sweet data here but how do i access it in the global scope?
console.log(source);
console.log(format);
return response;
},

// called on error
error: function(response, ioArgs){
console.error("HTTP status code: ",ioArgs.xhr.status);
return response;
}
});

}

var initTimeout = function(){
if(timer) { clearInterval(timer); } //clear the old timer just incase its still running
timer = setInterval(updateSettings, 5000); //check every 5 seconds.
}

dojo.addOnLoad(initTimeout);

// global scope -> I get 'source' is undefined
console.log(source);

// comparison statement here
if ( source == "xyz" ) { do something } else { do something else }

---

console.log(source) gets a

console.log(source) gets a "source is undefined" error because it is executing right after the setInterval call, but the code for the setInterval will not get called for another 5 seconds.

Put the console.log and the if (source == "xyz") check inside the load function for the xhrGet. Or, optionally, have the load: function call some other global function. At that point, source should be defined.

Thanks, i figured it out

Thanks, i figured it out before i read your reply. Murphy's law.
Anyway, I created a global function, then passed the source and format vars in as arguments, worked a treat.