2013-12-18 11 views
0

嗨,所以我正在學習AJAX的過程中,並決定在我的管理系統上有一個更好的UX用戶在線,消息,任務更新等,每30秒更新一次或者依賴於負載(在代碼中我將它設置爲5秒用於測試目的)。運行AJAX檢索登錄用戶返回undefined

PHP的文件工作正常,並輸出以下JSON:

[{ 「用戶名」: 「columkelly」, 「時間」: 「二○一三年十二月一十八日14時十三分55秒」}]

PHP

header('Content-Type: application/json'); 

if($_GET['function'] === "users_online"){ users_online(); } 

function users_online(){ 
$result=mysql_query("SELECT * FROM sessions"); 

while($array = mysql_fetch_assoc($result)){ 
    $dataArray[] = $array; 
} 
echo json_encode($dataArray); 
} 

問題是當我嘗試輸出是在線用戶......控制檯日誌顯示,它已經把它撿起來,但我無法得到它與該users_online功能工作回電話。這裏是AJAX:

AJAX

var timer, delay = 5000;   

timer = setInterval(function(){ 
val = $(this).serialize(); 
$(document).ready(function() { 
$.when(
    $.ajax({ 
     url: "ajax.php?function=users_online", 
     dataType: "json", 
     type: "GET", 
     data: val, 
     success: function(data) 
     { 
     console.log(data); 
     } 
    }), 
    $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?", { 
     tags: "bird", 
     tagmode: "any", 
     format: "json" 
    }) 
).then(function (users_online, images) { 
    $("#users_online").html(''); 
    $.each(users_online, function(data){ 
     $('#users_online').html(data.username +':' + data.time); 
    } 
), 
$("#dvImages").html(''); 
random = Math.floor((Math.random()*3)+1); 
$.each(images[0].items, function (i, item) { 
    var img = $("<img/>"); 
    img.attr('width', '200px'); 
    img.attr('height', '150px'); 
    img.attr("src", item.media.m).appendTo("#dvImages"); 
    if (i == random) return false; 
}) 
}); 
}); 
}, delay); 

我最終得到是來自閃爍API出現1-4的圖像(用這樣的一種試驗基)和未定義的:未定義。我知道它有東西做成功(我試圖將它們放入一個javascript數組,但沒有工作)和函數來獲取數據:

function (users_online, images) { 
    $("#users_online").html(''); 
    $.each(users_online, function(data){ 
     $('#users_online').html(data.username +':' + data.time); 
    } 
) 

感謝所有幫助迄今爲止球員。我已經搜索了很多,但我無法得到任何工作。

科拉姆

編輯:

這是工作的最終代碼:

var timer, delay = 5000;   
var users_online = []; 

timer = setInterval(function(){ 
val = $(this).serialize(); 
$(document).ready(function() { 
$.when(
    $.ajax({ 
     url: "ajax.php?function=users_online", 
     dataType: "json", 
     type: "GET", 
     data: val, 
     success: function(data) 
     { 
     console.log(data); 
     users_online = data; // Now call and loop through users_online wherever you need 
     } 
    }), 
    $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?", { 
     tags: "bird", 
     tagmode: "any", 
     format: "json" 
    }) 
).then(function (users_online, images) { 
    $("#users_online").html(''); 
    $.each(users_online[0], function (i, item) { 
     $('#users_online').html(item.username +':' + item.time); 
    } 
), 
$("#dvImages").html(''); 
random = Math.floor((Math.random()*3)+1); 
$.each(images[0].items, function (i, item) { 
    var img = $("<img/>"); 
    img.attr('width', '200px'); 
    img.attr('height', '150px'); 
    img.attr("src", item.media.m).appendTo("#dvImages"); 
    if (i == random) return false; 
}) 
}); 
}); 
}, delay); 
+0

您有一個變量範圍問題。來自'success:function(data){// data var的'data'只在這裏可用}'只在這些括號內可用 – MonkeyZeus

+0

因此創建一個全局變量數組並將數據插入到那個數組中?那會有用嗎? –

+0

參數'users_online'實際上是一個包含ajax成功回調參數的數組:[data,statusText,jqXHR]。因此,要循環訪問用戶,請使用$ .each(users_online [0],function(data){/ * ... * /})。 – Dragory

回答

1

雖然人會認爲這個解決方案如何優化,它會解決這個問題授予你不覆蓋它:

var timer, delay = 5000;   

var global_users_online = []; // create a global array 

timer = setInterval(function(){ 
val = $(this).serialize(); 
$(document).ready(function() { 
$.when(
    $.ajax({ 
     url: "ajax.php?function=users_online", 
     dataType: "json", 
     type: "GET", 
     data: val, 
     success: function(data) 
     { 
      console.log(data); 
      global_users_online = data; // Now call and loop through global_users_online wherever you need 
     } 
    }), 
    $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?", { 
     tags: "bird", 
     tagmode: "any", 
     format: "json" 
    }) 
) 
+0

上的示例我看到我的建議適用於您,但我只想提及代碼的幾個方面可以提高性能。我看到的一個例子是使用'$ .each(users_online){}'比'var length = users_online.length;要長50% for(var i = 0; i MonkeyZeus