Ajax显示为未定义 [英] Ajax appearing as undefined
问题描述
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:' + ' ' + value.availPCs + ' ' + 'out of' + ' ' + value.numPCs + '<br/>' + 'Macs available:' + ' ' + value.availMacs + ' ' + 'out of' + ' ' + 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屋!