如果這是一個重複的問題,我抱歉道歉,我已經從另一個似乎沒有幫助我的問題開始。我試圖檢索一些JSON數據,將部分數據存儲到變量中,並在AJAX請求之外的單獨函數中使用該變量。在單獨的函數中使用通過AJAX檢索的JSON數據
我從JSON數據預期的響應是http://localhost:8000/tutorials/retrieve/?page=2(此反應表明,如果我登錄的AJAX代碼內部的變量),但是實際的響應我收到的時候我嘗試登錄該變量從另一個函數如下:
n.Event {originalEvent:的MouseEvent,鍵入: 「點擊」,時間戳:1436727171161,jQuery21304066238570958376:真,toElement:DIV#loadmore.recentTutorials ...}
下面是當前的代碼
var recentFirstPage = '';
function retrieveTutorials(){
$.ajax({
type: "GET",
url: "/tutorials/retrieve",
dataType: "json",
success: function(data){
**some unrelated parsing code here**
//Set the variable to what I need
recentFirstPage = data.next_page_url;
},
error: function() {
alert("An error occurred processing AJAX request.");
}
});
}
$('#main-content-wrap').on('click', '.recentTutorials', function(recentFirstPage){
//Does not return expected result
console.log(recentFirstPage);
});
當我點擊.recentTutorials我希望控制檯從JSON記錄數據,但它不。有人可以幫助清理我的錯誤嗎?
哪裏是調用'retrieveTutorials'的代碼? –
@TonytheTech這是在頁面加載時運行的,並且鏈接到一個按鈕。 $(document).ready(function(){ \t retrieveTutorials(); }); – Dale