user715564
user715564

Reputation: 1690

getJSON returns "undefined" value

I am trying to learn how to make API calls with JSON and I am getting an "undefined" result. The API is for an invoice web app (www.curdbee.com) and I am wanting the results to be a list of each client name. Right now, I get a list but each result is "undefined". Here is my jQuery code so far:

$(function () {
    $.getJSON('https://nspirelab.curdbee.com/clients.json?api_token=', function (data) {
        $.each(data, function (index, item) {
            $('#results').append('<div class="invoice">' + item.name + '</div>');
        });
    });
});

Also, in the actual script, my api token is included.

Upvotes: 2

Views: 4121

Answers (1)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123473

According to the API docs for /clients, you're missing a property lookup -- the client of item.client.name.

$(function () {
    $.getJSON('https://nspirelab.curdbee.com/clients.json?api_token=', function (data) {
        $.each(data, function (index, item) {
            $('#results').append('<div class="invoice">' + item.client.name + '</div>');
        });
    });
});

This is needed to retrieve values of the inner objects:

[                                  // == data
    {                                  // == item
        "client": {                        // == item.client
            "id": 6364,                        // == item.client.id
            "name": "7Seven7 Insurance Inc",   // == item.client.name
            ...
        }
    },
    ...
]

Upvotes: 2

Related Questions