简体   繁体   English

遍历json列表时发生未定义的错误

[英]undefined error when iterating over json list

i got this code that keep on returning undefined msg instead of the expected html. 我得到了继续返回未定义msg而不是预期的html的这段代码。 Purpose of function: the purpose of the below function is to return notifications in away similar to fb one's. 函数的目的:以下函数的目的是在类似于fb的情况下返回通知。 the code is working fine. 该代码工作正常。 but there's something wrong with the getJSON part that i couldn't figure out. 但是getJSON部分出了点问题,我无法弄清楚。 so instead of returning "clonex1 likes your post" i get undefined. 因此,我没有得到定义,而不是返回“ clonex1喜欢您的帖子”。 the code 编码

function buzzme()
{
jQuery('#cnumber').removeClass();
jQuery('#cnumber').empty();

jQuery('#floating_box').toggle();

 var nHeader = '<div id="floating_box" class="fb">' +
'<div id="header"><span id="htext">Notifications</span></div>' +
'<div id="int">' +
'<div id="bodyx">' +
'<ul>';
var nFooter = '</ul>' +
'<div class="jfooter">' +
'<a href="#" id="seemore">See all notifications</a>' +
'</div>' +
'</div>' +
'</div>' +
'</div>';
var nContent;

jQuery.getJSON('notifications.php', {'n':1,'dht':3692}, function(response){
    jQuery.each(response, function(i, nt2){
        nContent += '<a href="#"><li id="lix">sdfsdfsd'+nt2.img+' '+nt2.notifier+'</li></a>';

    })

});

alert(nContent);
var nFinal = nHeader+nContent+nFooter;
if (!jQuery('#floating_box').length) {
  jQuery('body').append(nFinal);
}
}

notifications.php - setUpFlayout(); notifications.php-setUpFlayout(); and setUpJSONList() 和setUpJSONList()

    function setUpFlyout() {
    $notify = new se_notify();
    $data2 = $notify->notify_summary();
    $trk = 0;

    if($data2['total'] >= 1) {
    for($i = 0; $ $i <= $data2['total']; $i++) {
$nid = $data2['notifys'][$i]['notify_id'];
$im = $data2['notifys'][$i]['notify_icon'];
$img = "<img src='./images/icons/$im' />";
$notifier = $data2['notifys'][$i]['notify_text'][0];
$atype = $data2['notifys'][$i]['notifytype_id'];
$url = '';
$url2 = $data2['notifys'][$i]['notify_url'];
if($atype == 1) {
  $url = ' has sent you friend <a href='.$url2.'>request</a>';  
}
$trk++;    
if($data2['total'] >= 2) {
    $ret_arr = '';
     if($i == 0) {
         $ret_arr = '[';
     }
     $ret_arr = $ret_arr.setUpJSONList($data2['total'], $nid, $img, $notifier, $url, $trk);
     if($i == $data2['total']-1) {
         $ret_arr = $ret_arr.']';
     }
    echo ''; 
} else if($data2['total'] == 1){
   //$ret_arr = '[{"dh3":"'.$data2['total'].'","nid":"'.$nid.'", "img":"'.$img.'","notifier":"'.$notifier.'","url":"'.$url.'"}]';
   $ret_arr = '';
   echo $ret_arr;
}
    if($i == ($data2['total']-1))
        break;
        }
    }
}

setUpJSONList(); setUpJSONList();

function setUpJSONList($total, $nid, $img, $notifier, $url, $track) {
    $comma = ',';
    $lp = '';
    $rp = ']';
    $result = '';
    if($track == $total) {
    $result = '{"pos":"'.$track.'","dh3":"'.$total.'","nid":"'.$nid.'","img":"'.$img.'","notifier":"'.$notifier.'", "url":"'.$url.'"}';
    } else {
        $result = '{"pos":"'.$track.'","dh3":"'.$total.'","nid":"'.$nid.'","img":"'.$img.'","notifier":"'.$notifier.'", "url":"'.$url.'"},';    
    }
    return $result;
}

thanks 谢谢

Your usage of nContent after getJSON might be undefined since getJSON is asynchronous and would not have completed initializing nContent. 由于getJSON是异步的,并且尚未完成nContent的初始化,因此在getJSON之后对nContent的用法可能是不确定的。 You need to move the code using nContent inside the callback of getJSON. 您需要在getJSON的回调中使用nContent移动代码。

jQuery.getJSON('notifications.php', {'n':1,'dht':3692}, function(response){
    jQuery.each(response, function(i, nt2){
        nContent += '<a href="#"><li id="lix">sdfsdfsd'+nt2.img+' '+nt2.notifier+'</li></a>';

    })
    alert(nContent);
var nFinal = nHeader+nContent+nFooter;
if (!jQuery('#floating_box').length) {
  jQuery('body').append(nFinal);
}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM