0
我是一个完整的新手,所以也许一些答案必须解释一下。 我想做一个包含JSON字符串的小型html页面。该页面由我的本地网络上的Arduino托管(已测试和正在运行)。为了从JSON字符串中检索信息,我遵循了jQuery的视频教程:JSON AJAX jQuery tutorialJSON显示为“undefined”与jQuery
所以,我对草图进行了一些调整,以适应我的需求,但最后却出现了错误。在控制台中,我可以看到我获得了JSON,但在页面中,该值显示为“未定义”。
下面是代码:
$(function(){
var $channels = $('#channels'); //store array
$.ajax({
type: 'GET',
url: 'http://192.168.0.110/ajax_inputs',
success: function(channels){
$.each(channels, function(i, channel){
$channels.append('<p>channel # '+ channel.canal +' name: '+ channel.name +'</p>')
console.log(channels.canal)
});
},
error: function() {
alert('error loading data')
}
});
});
,这是JSON数据:
channels: [,…]
0: {name: "NAME 0", status: 1, canal: 0, temperature: -50, setPoint: 5, permission: 1, percentOut: 100}
1: {name: "NAME 1", status: 0, canal: 1, temperature: -50, setPoint: 5, permission: 0, percentOut: 0}
2: {name: "NAME 2", status: 0, canal: 2, temperature: -50, setPoint: 5, permission: 0, percentOut: 0}
3: {name: "NAME 3", status: 0, canal: 3, temperature: -50, setPoint: 5, permission: 0, percentOut: 0}
4: {name: "NAME 4", status: 0, canal: 4, temperature: -50, setPoint: 5, permission: 0, percentOut: 0}
5: {name: "NAME 5", status: 0, canal: 5, temperature: -50, setPoint: 5, permission: 0, percentOut: 0}
6: {name: "NAME 6", status: 0, canal: 6, temperature: -50, setPoint: 5, permission: 0, percentOut: 0}
7: {name: "NAME 7", status: 0, canal: 7, temperature: -50, setPoint: 5, permission: 0, percentOut: 0}
8: {name: "NAME 8", status: 0, canal: 8, temperature: 0, setPoint: 5, permission: 0, percentOut: 0}
9: {name: "NAME 9", status: 0, canal: 9, temperature: -50, setPoint: 5, permission: 0, percentOut: 0}
起初,它只是我使用的append()函数,并且它在页面上显示“undefine”。从教程中做同样的事情,一切都很好,直到我尝试访问json中的对象... – Nitrof