2017-02-24 84 views
1

我想在成功回調中訪問graphList,但graphList是未定義的。我提到了this StackOverflow後。但是,我無法訪問elem。任何幫助,將不勝感激。jquery:函數參數在成功回調中未定義

getGraphData = function (graphList) { 
$.ajax({ 
type: "GET", 
url: 'someURL', 
beforeSend: function(xhr){}, 
success: function(result) { 
    console.log(result); 
    console.log(graphList);//undefined 
} 
}); 
} 

enter image description here

+0

感謝,我也有,在實際的代碼。將更新 – user1556718

+0

在$ .ajax({'。之前檢查你的變量,因爲它似乎沒有錯,它應該工作 –

+0

graphList是未定義的,因爲graphList沒有進入你的函數 –

回答

0

新增創建如下getGraphList功能,我可以成功回調內部訪問getGraphList

var graphList; 
var promise = graphInit(response); 
promise.done(function(data){ 
    getGraphData(data.graphs) 
} 

graphInit = function(response,graphList){ 
    getGraphList = function(){return response.graphs;} 
} 

getGraphData = function (graphList) { 
    $.ajax({ 
    type: "GET", 
    url: 'someURL', 
    beforeSend: function(xhr){}, 
    success: function(result) { 
    console.log(result); 
    console.log(graphList);//undefined 
    graphList = getGraphList();//available 
    } 
}); 
}