Stack Overflow API - 端点问题

时间:2015-11-29 17:27:08

标签: javascript jquery api

我是API和编码的新手,所以我感谢所有的帮助。

我决定使用Stack Overflow API。 我想搜索特定主题的顶级Stack Overflow回答者。当我运行结果时,它会显示“未定义返回0结果”。我很确定我搞砸了我应该用来获取数据的部分。这就是我所拥有的。

你们有什么想法?

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);
        });
}

1 个答案:

答案 0 :(得分:1)

请查看this小提琴。希望这会有所帮助。

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');