weixin_33719619 2017-05-04 04:38 采纳率: 0%
浏览 194

Ajax显示为未定义

So I'm currently working on this website app and I'm trying to show how many PC's are available in my University using the JSON from the University website and AJAX. I can't figure it out but its showing all the MACs for the first room and then its saying undefined for others, its all one block of code so I can't see where its going wrong any advice would be great.

JS:

    var container = $('div.container');
$('input#get').click(function(){
    $.ajax({
        type: 'GET',
        url: '(mylink)',
        dataType: 'jsonp',
        success: function(data) {
        $.each(data, function(index, item) {
        $.each(item, function(key, value) {
            container.append('<div class="pcsinside">' + value.displayName + '</div>' + 'PCs available:' + '&nbsp' + value.availPCs + '&nbsp' + 'out of' + '&nbsp' + value.numPCs + '<br/>' + 'Macs available:' + '&nbsp' + value.availMacs + '&nbsp' + 'out of' + '&nbsp' + value.numMacs + '<br/>');
           });

          });

         }
     });
})

HTML:

<div class="container">
            <div id="form-area">
            <input id="get" type="submit" value="Press For PC Availability">
            </div>
            <div class="pcsinside">
            </div>
        </div>

This is what it looks like and it does update whenever a PC changes for the PC's and the MACs except for the undefined ones.enter image description here

It works for the first one but not for the other MACs, whats going on? Any help would be really appreciated thanks guys.

</div>
  • 写回答

2条回答 默认 最新

  • weixin_33674437 2017-05-04 04:44
    关注

    To display JSON as string you need to use JSON.stringify() function :

    container.append('<div>'+ JSON.stringify(value.displayName) +'</div>');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么