1

我有一个基于从 AJAX 调用收到的 JSON 数据填充的 JsTree。这是 AJAX 调用。

function sendQuery(){
    $.ajax({
        context: this,
        url: 'http://localhost:8080/testMain',
        type: 'GET',
        dataType: 'text',
        success: function(data) {
                       // ^^^^ Need for sendQuery() to return DATA 
                },
        error:  function (xhr, ajaxOptions, thrownError){    
                    alert('Error xhr : ' + xhr.status);    
                    alert('Error thrown error: ' + thrownError);    
                }
    }); 
}

我知道这里存在范围问题。在 JavaScript 中,变量是根据声明函数定义的。我只是不知道如何从 sendQuery() 返回,然后我将其用作另一个函数的参数,该函数将解析 JSON,这是另一个为树暂存的参数。有点沮丧,发条中的这篇文章不像我在 Java 中习惯的那样简单。非常感谢您的帮助,如果它有效,那么我肯定会接受。干杯

编辑#1:好的,根据答案,我相信如果我以这种方式更改我的代码,它将允许数据从 .ajax 函数中取出。仍然存在如何将其重新纳入程序流程的问题。

function sendQuery(){
    $.ajax({
        context: this,
        url: 'http://localhost:8080/testMain',
        type: 'GET',
        dataType: 'text',
        success: getJson,
        error:  function (xhr, ajaxOptions, thrownError){    
            alert('Error xhr : ' + xhr.status);    
            alert('Error thrown error: ' + thrownError);    
        }
    }); 
}

function getJson(data){
    alert("Transmission Success.");
    alert(data);
    var obj = $.parseJSON(data);
    alert("Parsing JSON Success.");
    var apples = obj.apples;
    alert(apples);
    return apples;
}

好的,那么现在我如何将 APPLES 变量放入调用链中,该调用链将为树暂存数据?

我需要将 APPLES 变量提供给将处理数据的函数。

编辑#2 使用回调:

我花了一秒钟的时间来思考回调的想法。这是我能用它做的。

这是我的原始树代码,它调用一系列函数来做不同的事情,但最终以树接受的形式获取数据。

$(function () {     
    $("#client_tree").jstree({
        "json_data": {"data": attachTree(stageTreeData(getJson(sendQuery())))}, 
        "plugins" : [ "themes", "json_data", "ui" ]
    }).bind("select_node.jstree", function (e, data) { 
        var msg = data.rslt.obj.attr("id");
        alert(msg);
    });
});

我目前正在尝试通过 Ajax 在 sendQuery() 方法中获取数据,然后从它返回数据等...]

I changed it slightly, now I dont call sendQuery(), jQuery calls it.

$(function (){
    $.ajax({
        context: this,
        url: 'http://localhost:8080/testMain',
        type: 'GET',
        dataType: 'text',
        success: loadTree,
        error:  function (xhr, ajaxOptions, thrownError){    
            alert('Error xhr : ' + xhr.status);    
            alert('Error thrown error: ' + thrownError);    
        }
    });
});

Also changed my tree loading code a bit...

function loadTree(data){
    $("#client_tree").jstree({
        "json_data": {"data": attachTree(stageTreeData(getJson(data)))},    
        "plugins" : [ "themes", "json_data", "ui" ]
    }).bind("select_node.jstree", function (e, data) { 
        var msg = data.rslt.obj.attr("id");
        alert(msg);
    });

}

I have no errors, no exceptions and the tree is populated.

Thank you all for helping!

EDIT#3 Fixing some minor stuff:

Moved to Alert() call in jQuery not displaying, called from within a JsTree

4

4 回答 4

3

No, you don't need that.

What you need is to use the data.

But you can't return them from sendQuery, because the call is asynchronous, which means the data are available only after sendQuery has returned.

The solution is to provide your sendQuery function a callback that will do what you want to do with the data when they're available :

function sendQuery(callback){
   ...
   success: callback,
   ...
}

...
sendQuery(function(data){
    // do things with data
});
于 2012-10-21T18:56:39.007 回答
2

The problem with what you're trying to do is that the ajax call is asynchronous. The sendQuery function will return, and control flow will continue, before you've gotten a response from the server.

The way to use it is using callbacks. When you get a response back from the server, the function that you've passed to success() will get called. Basically, you need that function to pick up your processing pipeline where you left off. You want that function to parse the response into json, "stage for the tree", etc.

You'll need to separate the function that calls this into what happens before the call, and then "everything else" that happens after the call comes back. That "everything else" is what you want in the success callback.

Example (making some assumptions about who is calling this function)

function doQueryAndParseData(){  //This is the function that calls doQuery
   prepare_something();
   //You pass this in as a callback, since it can only happen once you have data
   sendQuery(function(data){     
       parsed_data = JSON.parse(data);  //This is where you do your work
       //Put parsed data into dom somehow
   }
   return; //This function will return before the data gets back, but when the server responds the data will get parsed and put into the tree
}

function sendQuery(callback){
    $.ajax({
        context: this,
        url: 'http://localhost:8080/testMain',
        type: 'GET',
        dataType: 'text',
        success: callback,
        error:  function (xhr, ajaxOptions, thrownError){    
                    alert('Error xhr : ' + xhr.status);    
                    alert('Error thrown error: ' + thrownError);    
                }
    }); 
}    
于 2012-10-21T19:08:11.487 回答
1

You need to consume the data within the AJAX success. Can use another function as this code shows

   function sendQuery(){
        $.ajax({
            context: this,
            url: 'http://localhost:8080/testMain',
            type: 'GET',
            dataType: 'text',
            success: loadTree,

            error:  function (xhr, ajaxOptions, thrownError){    
                        alert('Error xhr : ' + xhr.status);    
                        alert('Error thrown error: ' + thrownError);    
                    }
        }); 
    }


function loadTree( data){
   /* do something with data returned from ajax*/

}
于 2012-10-21T19:09:20.987 回答
-2
function sendQuery(){
    var val;
    $.ajax({
        context: this,
        url: 'http://localhost:8080/testMain',
        type: 'GET',
        async: false,
        dataType: 'text',
        success: function(data) {
                       val = data;
                },
        error:  function (xhr, ajaxOptions, thrownError){    
                    alert('Error xhr : ' + xhr.status);    
                    alert('Error thrown error: ' + thrownError);    
                }
    }); 
    return val;
}
于 2012-10-21T18:59:09.890 回答