2012-10-21 44 views
1

我有一個JsTree,它基於從AJAX調用中接收到的JSON數據獲取。這是AJAX調用。我需要從jQuery中的.ajax函數「success」中返回

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的函數,該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()方法,然後將其與數據等返回...]

我改變了它咯,現在我不來電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); 
    }); 

} 

我沒有錯誤,沒有例外並填充樹。

謝謝大家的幫忙!

編輯#3修復了一些小東西:

移到Alert() call in jQuery not displaying, called from within a JsTree

回答

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*/ 

} 
+0

哦,所以如果我傳遞一個像success這樣的函數:functionName它會將數據傳回給函數。太棒了。非常感謝。我會盡力。 –

+1

yes ..'success:functionName'將與'success:function(data){functionName(data)}相同' – charlietfl

+0

OK剛剛對問題進行了編輯,我需要將此數據發送給函數調用。有空的時候請檢查一下。謝謝 –

3

不,你不需要這個。

你需要的是使用這些數據。

但是你不能從sendQuery返回它們,因爲這個調用是異步的,這意味着數據只有在sendQuery返回後纔可用。

的解決方案是提供您sendQuery功能的回調,會做你想要時,他們提供的數據做什麼:跟你想什麼

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

... 
sendQuery(function(data){ 
    // do things with data 
}); 
+0

好的,所以我真正需要做的就是調用sendQuery()並存儲響應。然後調用另一個將返回值的函數。對? –

+0

您需要調用'sendQuery','sendQuery'將調用其他函數。 –

-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; 
} 
+2

請勿使用async:false。它被棄用,阻止瀏覽器,並且總是一個非常糟糕的主意。 –

2

問題do是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);  
       } 
    }); 
}  
+0

我喜歡你的答案,我可能能夠將呼叫包裝在另一個函數中,當被調用時會將成功結果返回給我。謝謝 –

+0

所以如果我需要doQueryAndParseData()來返回parsed_data生病需要另一個回調? –

+1

無法將函數調用發送到網絡,然後返回解析的數據(除了async:false之外,您不希望這樣做,因爲它會凍結頁面的其餘部分)。原因是,直到你的javascript返回(從頂級事件或頁面加載),網絡調用將不會完成。爲了有效地編寫javascript,你將需要使用一種異步IO風格來影響整個程序結構,而不僅僅是你所做的調用的外觀。 – Eric