Trendy
Trendy

Reputation: 470

Return String From XHRGet

I'm creating a tree using Dojo and two seperate sets of data. One set of data makes up the main tree structure. The second set of data is dependent on a value from the first set. I'm using xhrGet and Dojo 1.7.3 to get the data.

Once the second set of data has been returned, I'm looking at the values of the JSON to determine a value of a variable, that's then passed into the tree. The variable displays a "!" if an "alert" value is present in the JSON returned and blank if there isn't.

var theAlert = dojo.xhrGet({
    url: proxy + serviceurl + targetId,
    handleAs: 'json',
    load: function(data){
            if(typeof data.alerts[0] != 'undefined'){
                    var hello = "!";
                    return hello;
            }
            else{
                    console.log("There is nothing there");
            },
    error: function(error){
            console.log(error)
            }
    });

The problem I'm having is that when I write "theAlert" variable where I need to, it appears as "[object Object]" and not as "!".

I feel like I'm doing something wrong, but I can't figure out what.

I have already tried using theAlert.valueOf(); to no success. Help?

The data is received correctly as well, I can view it via console log.

Upvotes: 0

Views: 88

Answers (2)

denov
denov

Reputation: 12698

dojo.xhrGet() returns a Deferred - http://dojotoolkit.org/reference-guide/1.9/dojo/Deferred.html

You need to do something like:

var deferred = dojo.xhrGet({
  url: proxy + serviceurl + targetId,
  handleAs: 'json'
});

deferred.then(
    function(data){
        if(typeof data.alerts[0] != 'undefined'){
           processAlert("!");
        } else{
           console.log("There is nothing there");
        }
    },

    function(error){
        console.log(error)
    }
);

function processAlert(a) {
   alert(a);
} 

Upvotes: 1

Rob Adams
Rob Adams

Reputation: 450

Look at the docs.

You need to return data, not hello.

Upvotes: 0

Related Questions