getJSON returns error 'object is undefined'
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
开发者_StackOverflow社区 },
{
"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
Your data doesn't seem to have a results
property. Did you mean project
?
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) :)
精彩评论