dougong5285 2013-12-18 14:48
浏览 68
已采纳

运行AJAX以检索登录用户返回undefined

Hi so I am in the process of learning AJAX and have decided for a better UX on my admin system to have users online, messages, tasks updated and so on updated every 30 seconds or so depending on the load (in the code i have it set to 5 seconds for testing purposes).

The PHP file works fine and outputs the following JSON:

[{"username":"columkelly","time":"2013-12-18 14:13: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);
}

The problem comes when I try to output the users that are online... The console log shows that it has picked it up but I am unable to get it to work with the users_online function with the callback. Here is the 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);

What i end up getting is 1-4 images appearing from the flicker api (used this as a test base) and undefined:undefined. I know it has to have something to do with the success (i tried putting them into a javascript array but that did not work) and the function to grab the data:

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

Thanks for all the help so far guys. I have searched far and wide but am unable to get anything to work with this.

Colum

EDIT:

This is the finished code that worked:

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);
  • 写回答

1条回答 默认 最新

  • duanniu4106 2013-12-18 15:03
    关注

    Although people would argue how optimal this solution is, it will fix the issue granted you don't overwrite it:

    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"
        })
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮