开发者

getJSON returns error 'object is undefined'

开发者 https://www.devze.com 2023-03-08 09:31 出处:网络
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)

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) :)

0

精彩评论

暂无评论...
验证码 换一张
取 消