Stack Overflow API - Trouble with Endpoint

I am new to API's and coding in general, so I appreciate all help.

I decided to use the Stack Overflow API. I want to search for top Stack Overflow answerers for particular topics. When I run the results, it says "0 results returned for undefined." I'm pretty sure that I have screwed up the part that I am supposed to use for getting data. This is what I have.

What do you guys think?

JavaScript:

var showSearchResults = function(query, resultNum) {
    var results = resultNum + ' results for <strong>' + query + '</strong>';
    return results;
};

var getAnswerers = function (answered) {
    //the parameters I need to pass in our request to StackOverflow's API
    var request = {
        tagged: answered,
        site: 'stackoverflow',
        order: 'desc',
        sort: 'creation'
    };

    $.ajax({
        //the parameters I need to pass in our request to stackOverFlow's API
        //this is the endpoint that I want to use
        url: "http://api.stackexchange.com/2.2/tags/{tag}/top-answerers/all_time",
        data: request,
        dataType: "jsonp",
        type: "GET",
    })
    //what the function should do if successful
        .done(function (result) {
            //this gives you the search result description
            var searchResults = showSearchResults(request.tagged, result.items.length);

            $('.search-results').html(searchResults);
            $.each(result.items, function (i, item) {
                var question = showQuestion(item);
                $('.results').append(question);
            });
        })
    //what the function should do if it fails
        .fail(function (jqXHR, error) {
            var errorElem = showError(error);
            $('.search-results').append(errorElem);
        });
}

Upvotes: 2

Views: 156

Answers (1)

Raman
Raman

Reputation: 1281

Please have a look at this fiddle. Hope this helps.

var getAnswerers = function(answered){
    //the parameters I need to pass in our request to StackOverflow's API
    var request = { 
        tagged: answered,
        site: 'stackoverflow',
        order: 'desc',
        sort: 'creation'
    };

    $.ajax({
        //the parameters I need to pass in our request to stackOverFlow's API
        //this is the endpoint that I want to use
        url: "http://api.stackexchange.com/2.2/tags/"+answered+"/top-answerers/all_time",
        data: request,
        dataType: "jsonp",
        type: "GET",
        })
    //what the function should do if successful
        .done(function(result){
            //this gives you the search result description
        debugger;
            /*var searchResults = showSearchResults(request.tagged, result.items.length);

            $('.search-results').html(searchResults);
            $.each(result.items, function(i,item){
                var question = showQuestion(item);
                $('.results').append(question);
            });*/
        })
    //what the function should do if it fails
        .fail(function(jqXHR, error){
        debugger;
           /* var errorElem = showError(error);
            $('.search-results').append(errorElem);*/
        });
}

getAnswerers('ruby');

Upvotes: 1

Related Questions