Ajax显示为未定义 [英] Ajax appearing as undefined

查看:267
本文介绍了Ajax显示为未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我目前正在研究这个网站的应用程序,并且我正在尝试使用来自大学网站和AJAX的JSON来显示我的大学中有多少台PC可用。我无法弄清楚,但它显示了第一个房间的所有MAC,然后它的说法对其他人来说是未定义的,它的所有代码块,所以我不能看到它出错的地方,任何建议都会很棒。



JS:

  var container = $('div.container ); 
$('input#get')。click(function(){
$ .ajax({
type:'GET',
url:'(mylink)',
dataType:'jsonp',
成功:函数(数据){
$ .each(数据,函数(索引,项目){
$ .each(item,function键值,值){
container.append('< div class =pcsinside>'+ value.displayName +'< / div>'+'PC available:'+'& nbsp'+ value.availPCs +'& nbsp'+'out of'+'& nbsp'+ value.numPCs +'< br />'+'Macs available:'+'& nbsp'+ value.availMacs + '& nbsp'+'out of'+'& nbsp'+ value.numMacs +'< br />');
});

});

$ b});
})

HTML:

 < div class =container> < div id =form-area> < input id =gettype =submitvalue =Press For PC Availability> < / DIV> < div class =pcsinside> < / DIV> < / div>  

这就是它的样子不管更新PC和MAC,除了未定义的PC之外, https://i.stack.imgur.com/EXv5j.pngalt =在这里输入图片描述>



它适用于第一个一个但不是为其他MAC,什么事情发生?任何帮助将非常感谢的家伙。

解决方案

首先在dataType中,你把'jsonp',而不是'json'。而且Jayesh是对的,你必须扎扎实实地看到答案。看看这个例子。

  $。ajax({
url:blabla.php,
data :data,
type:'POST',
contentType:application / x-www-form-urlencoded,
dataType:'json',
error:function(data ){
var err = JSON.stringify(data);
alert(err);
},
success:function(data){
var succ = JSON .stringify(data);
alert(succ);
}
});

通过这种结构,您会发现问题所在。使用后,而不是得到,它更好。


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.

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

解决方案

First in dataType you put 'jsonp' instead of 'json'. And S Jayesh is right, you must stringify to see the answer. See this exemple.

$.ajax({
    url: "blabla.php",
    data: data,
    type: 'POST',
    contentType: "application/x-www-form-urlencoded",
    dataType: 'json',
    error: function(data){
        var err = JSON.stringify(data);
alert(err);
    },
    success: function(data){
        var succ = JSON.stringify(data);
alert(succ);
    }
});

With that structure, you'll see what is the problem. Use post instead of get, it's better.

这篇关于Ajax显示为未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆