getJSON 返回错误“对象未定义”
我在尝试获取 json 数据时遇到了这个麻烦。
假设我有一个名为“projects.json”的文件,其结构如下(显示 2 个项目,原始文件有超过 100 个),
{"project":[
{
"featid":1,
"ced":12001,
"x":659770.164751449,
"y":990679.029463668
},
{
"featid":2,
"ced":110002,
"x":621482.834052153,
"y":1034455.00718159
}
]
}
当我尝试访问数据时,出现以下错误,
object is undefined
length = object.length,
这是我用来获取数据
$(document).ready(function(){
var url="json/projects.json";
$.getJSON(url,{featid: 1},function(data){
$.each(data.results,function(i,proy){
$("#output").append('<p>'+proy.ced+'</p>');
});
});
});
我遗漏了什么?我检查了 json 文件,似乎是有效的,所以我不知道它是什么。
感谢您的帮助
i have been having this trouble when trying to obtain json data.
lets say i have a file called "projects.json" with the following structure (showing 2 items, the original file has over 100)
{"project":[
{
"featid":1,
"ced":12001,
"x":659770.164751449,
"y":990679.029463668
},
{
"featid":2,
"ced":110002,
"x":621482.834052153,
"y":1034455.00718159
}
]
}
when i try to acces the data i get the following error
object is undefined
length = object.length,
this is the function i am using to obtain the data
$(document).ready(function(){
var url="json/projects.json";
$.getJSON(url,{featid: 1},function(data){
$.each(data.results,function(i,proy){
$("#output").append('<p>'+proy.ced+'</p>');
});
});
});
i am missing something? i checked the json file and appears to be valid, so i dont know what could it be.
thank you for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的数据似乎没有
results
属性。您指的是项目
吗?Your data doesn't seem to have a
results
property. Did you meanproject
?阿特斯是对的。不要使用
$.each(data.results,function(i,proy)
使用
$.each(data.project,function(i,proy)
因为你的 JSON 有项目没有结果。看来您已经从任何示例复制了代码并且忘记更改变量(这很常见):)
Ates is right. Instead of using
$.each(data.results,function(i,proy)
use
$.each(data.project,function(i,proy)
as your JSON have project not results. It seems you have copied the code from any example & forgot to change the variable (which is very common) :)