Print

Print


Are you trying to run this inside a webpage served from a domain other than
nytimes.com?
If so, you'd need to use JSONP, which a cursory examination of their API
documentation reveals they do not support. So, you need to use a proxy.

Here's one:
$ cat hardcover.php
<?
$cb = @$_GET['callback'];

$json = file_get_contents('
http://api.nytimes.com/svc/books/v2/lists/hardcover-fiction.json?&api-key=xxxx'
);
header("Content-Type: text/javascript");
echo $cb . '(' . $json . ')';

?>

Install it on your webserver, then change your JavaScript code to refer to
it using callback=?.

For instance, if you installed it on
http://libx.lib.vt.edu/services/nytimes/hardcover.php
then you would be using the URL
http://libx.lib.vt.edu/services/nytimes/hardcover.php?callback=?
(.getJSON will replace the ? with a suitably generated function name).

 - Godmar

On Wed, Sep 28, 2011 at 3:28 PM, Nate Hill <[log in to unmask]> wrote:

> Anybody out there using the NY times best seller API to do stuff on their
> library websites?
> I can't figure out what's wrong with my code here.
> Data is returned as "null"; I can't seem to parse the response with jQuery.
> Any help would be supercool.
> I removed the API key - my code doesn't actually contain 'xxxx'.
> Here's the jQuery:
>
> jQuery(document).ready(function(){
>    $(function(){
>                    //json request to new york times
>                    $.getJSON('
>
> http://api.nytimes.com/svc/books/v2/lists/hardcover-fiction.json?&api-key=xxxx
> ',
>
>                    function(data) {
>                        //loop through the results with the following
> function
>                        $.each(data.results.book_details, function(i,item){
>                        //turn the title into a variable
>                        var bookTitle = item.title;
>                        $('#container').append('<p>'+bookTitle+'</p>');
>
>            });
>        });
>    });
> });
>
>
> Here's a snippet of the JSON response:
>
> {
>    "status": "OK",
>    "copyright": "Copyright (c) 2011 The New York Times Company.  All Rights
> Reserved.",
>    "num_results": 35,
>    "last_modified": "2011-09-23T12:00:29-04:00",
>    "results": [{
>        "list_name": "Hardcover Fiction",
>        "display_name": "Hardcover Fiction",
>        "updated": "WEEKLY",
>        "bestsellers_date": "2011-09-17",
>        "published_date": "2011-10-02",
>        "rank": 1,
>        "rank_last_week": 0,
>        "weeks_on_list": 1,
>        "asterisk": 0,
>        "dagger": 0,
>        "isbns": [{
>            "isbn10": "0399157786",
>            "isbn13": "9780399157783"
>        }],
>        "book_details": [{
>            "title": "NEW YORK TO DALLAS",
>            "description": "An escaped child molester pursues Lt. Eve
> Dallas; by Nora Roberts, writing pseudonymously.",
>            "contributor": "by J. D. Robb",
>            "author": "J D Robb",
>            "contributor_note": "",
>            "price": 27.95,
>            "age_group": "",
>            "publisher": "Putnam",
>            "primary_isbn13": "9780399157783",
>            "primary_isbn10": "0399157786"
>        }],
>        "reviews": [{
>            "book_review_link": "",
>            "first_chapter_link": "",
>            "sunday_review_link": "",
>            "article_chapter_link": ""
>        }]
>
>
> --
> Nate Hill
> [log in to unmask]
> http://www.natehill.net
>