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 では、変数は宣言関数に従って定義されます。JSONを解析する別の関数へのパラメーターとして使用している sendQuery() から戻る方法がわかりません。これは、別のパラメーターがツリーのステージングを行うためのパラメーターです。時計仕掛けのこの部分が、私が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 変数を渡す必要があります。

EDIT #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()メソッドで、データなどを返します...]

少し変更しました。今は sendQuery() を呼び出さず、jQuery が呼び出します。

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

また、ツリーの読み込みコードを少し変更しました...

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

}

エラーも例外もありません。ツリーにはデータが取り込まれています。

助けてくれてありがとう!

EDIT#3いくつかのマイナーなものを修正:

JsTree内から呼び出され、表示されない jQuery の Alert() 呼び出しに移動

4

4 に答える 4

3

いいえ、必要ありません。

必要なのは、データを使用することです。

ただし、呼び出しは非同期であるため、sendQuery からそれらを返すことはできません。つまり、sendQuery が返された後にのみデータを使用できることを意味します。

sendQuery解決策は、データが利用可能になったときにデータでやりたいことを行うコールバックを関数に提供することです。

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

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

あなたがしようとしていることの問題は、ajax 呼び出しが非同期であることです。サーバーから応答を受け取る前に、sendQuery 関数が戻り、制御フローが続行されます。

それを使用する方法は、コールバックを使用することです。サーバーから応答が返されると、渡した関数がsuccess()呼び出されます。基本的に、中断したところから処理パイプラインを取得するには、その関数が必要です。その関数で応答を json、「ツリーのステージ」などに解析する必要があります。

これを呼び出す関数を、呼び出しの前に発生することと、呼び出しが戻った後に発生する「その他すべて」に分ける必要があります。その「その他すべて」は、成功のコールバックで必要なものです。

例 (誰がこの関数を呼び出しているかについての仮定)

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

AJAX 成功内でデータを使用する必要があります。このコードが示すように、別の関数を使用できます

   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 に答える