连接New York Times API JSON数据以HTML格式显示

时间:2014-05-26 22:00:58

标签: javascript html json api each

我现在正在使用javascript开发API项目,而且我无法找到任何关于如何将纽约时报API中的json数据放入我的html的答案。我已成功链接到the NYT API

在下面的js中,findSearch()函数成功从纽约站点获取数据,但当我使用$.each()调用showData(i, result)函数时,数据会返回未定义。

我可以看到,由于我在console.log()函数中执行了showData(),因此数据正在使其达到此功能,但我认为我正在错误地调用json数据。

要查看整个项目(不是很大,不要担心),你can go here

var showData = function (i, result) {

var template = $(".results .template .searchDisplay").clone();

var headline = template.find('.headline');
headline.text(result.response.docs.headline);

var abstract = template.find('.abstract');
abstract.text(result.response.docs.abstract);

var snippet = template.find('.snippet');
snippet.text(result.response.docs.snippet); 

var url = template.find('.url');
url.text(result.response.docs.url);

console.log(result)
console.log(result.response.docs.headline)

$('.searchDisplay').show('fast');

return result;
};


// THIS FUNCTION CALLS OUT TO THE NYT API, GETS DATA
var findSearch = function(search){

var request =   {tagged: search,
                site: 'New York Times',
                order: 'decs',
                sort: 'creation'};

var result = $.ajax({
    url: 'http://api.nytimes.com/svc/search/v2/articlesearch.json?q=' + search + '&fq=source:("The New York Times")&page=0&sort=oldest&api-key=a1eeb62c8df499298c449983e6967154:3:69423736',
    type: 'GET',
    dataType: 'json',
    data: search,
})
.done(function(result) {
    console.log("success");
    console.log(result.response);
    var searchResults = showSearchData(request.tagged, result.response.docs.length );
    $('.info').html(searchResults);

    $.each(result.response.docs, function(i, item) {
        var showing = showData(item, result);
        $('.searchDisplay').append(showing);
    });
}).fail(function() {
    console.log("error");
    $('.results').html('This feature is not working. :-(');
});

0 个答案:

没有答案