0

I have the following JSon

{
"expertises": {
    "leadership": {
        "detail1": "+++++",
        "detail2": "++++",
        "detail3": "+++"
    },
    "Project management": {
        "detail1": "++++",
        "detail2": "+++++"
    }
}

}

and the following code.

var data = $.ajax({
    url: 'js/expertise.json',
    dataType: "json",
    success: function(data){
        $.each(data.expertises, function(key, value){
            if(name == key){
                return value;
            }
        });
    },error: function(request, error){
        console.log("a wild error has occured! " + error);
    }
});

When I run this it reads the json just fine and it loops through it just fine, but when it gets to the last each cycle it pushes the error out. any ideas what I am missing here. I have tried using arrays surrounding the objects but to no avail.

  • How do you know it "loops through it just fine"? The function doesn't do anything visible. (In fact `return value` probably doesn't do what you think it does.) Which line gives the error? – JJJ Jan 20 '15 at 11:39
  • well the error is generated after the loop? somehow. I can even put another each statement in the if and loop through the value, and it presents the details keys and values. – user2051112 Jan 20 '15 at 12:16
  • You should generally show the line that throws the error when asking questions. In any case this looks like a duplicate of [How to return the response from an Ajax call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) -- `data` will always be undefined. – JJJ Jan 20 '15 at 12:38
  • Also, if you're looping through the response just to find the key you want, that's totally unnecessary. You can just do `data.expertises[ name ]`. – JJJ Jan 20 '15 at 12:40
  • @juhana thats a good call. as for the error, its generated in the Jquery line. "Uncaught TypeError: Cannot read property 'length' of undefined - jquery.js:564" – user2051112 Jan 21 '15 at 02:00

0 Answers0