0

I've just recently started getting into some ajax, and I'm trying to figure out how to pull data from a website using jsonp. I'm trying to pull the jQuery github account information and display it in the browser. The only problem being I keep receiving an undefined for the two values I am trying to pull out of each array in the json data. When I go to the console to error check, I can clearly see all of the json data file information.Another thing is that while there are many different entries, the each loop only does two iterations before it stops printing. My jQuery code is below.

var url = 'https://api.github.com/users/jquery/repos';
$('#letter-h a').click(function(event){
event.preventDefault();
$.getJSON(url + '?callback=?', function(data){
        var html = '';
        $.each(data, function(entryIndex, entry){
            html += '<div class="entry">';
            html += '<h3 class="term">' + entry.id + '</h3>';
            html += '<div class="part">' + entry.name + '</div>';
            html += '</div>';
        });
        $('#dictionary').html(html);
    });
});

I have looked through many different questions on this site (as this question apparently gets asked often) to try to assist but none of the suggestions have worked. This includes entry[entryIndex].id, data.entry.id, data.id.

1 Answer 1

1

The github api seems to respond differently based on response type (JSONP vs JSON). For json, it responds [...]. For jsonp, it responds {data:[...], meta:{...}}.

I suggest just adding:

data = data.length ? data : data.data;

to the first line of your callback.

Sign up to request clarification or add additional context in comments.

1 Comment

it worked! Thank you! So what you're checking for is if there is a length, then data remains; if not, add the data onto the end of it to test for api variability?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.