0

我正在使用 Dojo 和两组单独的数据创建一棵树。一组数据构成主树结构。第二组数据取决于来自第一组的值。我正在使用 xhrGet 和 Dojo 1.7.3 来获取数据。

返回第二组数据后,我将查看 JSON 的值以确定变量的值,然后将其传递到树中。变量显示“!” 如果返回的 JSON 中存在“警报”值,如果没有则为空白。

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)
            }
    });

我遇到的问题是,当我在需要的地方编写“theAlert”变量时,它显示为“[object Object]”而不是“!”。

我觉得我做错了什么,但我不知道是什么。

我已经尝试过使用 theAlert.valueOf(); 没有成功。帮助?

数据也被正确接收,我可以通过控制台日志查看它。

4

2 回答 2

1

dojo.xhrGet() 返回一个 Deferred - http://dojotoolkit.org/reference-guide/1.9/dojo/Deferred.html

您需要执行以下操作:

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);
} 
于 2013-07-10T21:48:51.987 回答
0

查看文档

您需要返回数据,而不是您好。

于 2013-07-10T19:09:07.680 回答