Because a few people have asked me about this off list, and because I'm now
struggling with a different related question, I'm bringing it back to the
list for all.
If it's TMI, just delete the email and move on... sorry to jam your inbox.
But I'm determined to make this thing work.
I have the proxy working fine as Godmar suggested, but I'm doing something
wrong parsing the JSON.
Thanks- I learn an awful lot by interacting with this list.
*Here's the PHP for the proxy:*
<?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 . ')';
?>
*Here's the jQuery:*
jQuery(document).ready(function(){
$(function(){
//json request to new york times
$.getJSON('
http://www.sjpl.org/otherscripts/callback.php?callback=?',
function(data) {
//loop through the results with the following
function
$.each(data.results[0].book_details,
function(i,item){
//turn the title into a variable
var bookTitle = item.title;
$.each(data.results[0].isbns, function(i,item){
//turn the isbn into a variable
var bookIsbn = item.isbn13;
$('#container').append('<a href="
http://catalog.sjlibrary.org/search~/a?searchtype=i&searcharg='+bookIsbn+'&SORT=D&searchscope=1">'+bookTitle+'</a><br>');
});
});
});
});
});
*Here's my problem:*
I can only return the book title and the isbn by specifying which one in the
array I want, so I'm using: data.results[0].book_details to pull the first
title.
Something is wrong with the way I'm using $.each()
If I remove [0] and have data.results.book_details I get no result.
If I add [1] and have data.results[1].book_details I get the same result
twice.
If I add [2] and have data.results[2].book_details I get the same result
three times.
I'm sure I'm doing something dumb with my syntax. Any ideas?
*Here's a bigger chunk of the JSON, since I'm having problems moving through
it.*
{
"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": ""
}]
}, {
"list_name": "Hardcover Fiction",
"display_name": "Hardcover Fiction",
"updated": "WEEKLY",
"bestsellers_date": "2011-09-17",
"published_date": "2011-10-02",
"rank": 2,
"rank_last_week": 0,
"weeks_on_list": 1,
"asterisk": 0,
"dagger": 0,
"isbns": [{
"isbn10": "0385534639",
"isbn13": "9780385534635"
}, {
"isbn10": "0385534647",
"isbn13": "9780385534642"
}],
"book_details": [{
"title": "THE NIGHT CIRCUS",
"description": "Two young rivals at a magical circus become
collaborators as they fall in love.",
"contributor": "by Erin Morgenstern",
"author": "Erin Morgenstern",
"contributor_note": "",
"price": 26.95,
"age_group": "",
"publisher": "Doubleday",
"primary_isbn13": "9780385534635",
"primary_isbn10": "0385534639"
}],
"reviews": [{
"book_review_link": "",
"first_chapter_link": "",
"sunday_review_link": "",
"article_chapter_link": ""
}]
}, {
"list_name": "Hardcover Fiction",
"display_name": "Hardcover Fiction",
"updated": "WEEKLY",
"bestsellers_date": "2011-09-17",
"published_date": "2011-10-02",
"rank": 3,
"rank_last_week": 1,
"weeks_on_list": 3,
"asterisk": 0,
"dagger": 0,
"isbns": [{
"isbn10": "0316097543",
"isbn13": "9780316097543"
}, {
"isbn10": "1607884674",
"isbn13": "9781607884675"
}, {
"isbn10": "0316097551",
"isbn13": "9780316097550"
}],
"book_details": [{
"title": "KILL ME IF YOU CAN",
"description": "When a young man finds a bag of diamonds, he
gets the attention of the major assassin the Ghost, and a rival assassin who
wants the Ghost gone forever.",
"contributor": "by James Patterson and Marshall Karp",
"author": "James Patterson and Marshall Karp",
"contributor_note": "",
"price": 27.99,
"age_group": "",
"publisher": "Little, Brown",
"primary_isbn13": "9780316097543",
"primary_isbn10": "0316097543"
}],
"reviews": [{
"book_review_link": "",
"first_chapter_link": "",
"sunday_review_link": "",
"article_chapter_link": ""
}]
},
On Wed, Sep 28, 2011 at 2:02 PM, Michael B. Klein <[log in to unmask]> wrote:
> You can pull data from their API into a server-side process and then pass
> it
> along (filtered or raw) to your browser. But browser security won't let you
> access JSON data from a different-origin server.
>
> It's not NYTimes.com's fault; it's the cross-site scripting jerks who made
> the security necessary in the first place.
>
> On Wed, Sep 28, 2011 at 1:26 PM, Nate Hill <[log in to unmask]>
> wrote:
>
> > Wait- what would be the point of their API if I couldn't run anything on
> a
> > domain other than nytimes.com?
> > Thanks everyone for the pointers. I'll get back to it!
> > If I can pull the first 5 titles from the different best seller lists,
> and
> > then using the ISBN build a link to those titles in the library catalog,
> I
> > will have made something useful which I will gladly share back to the
> list.
> >
> > On Wed, Sep 28, 2011 at 1:23 PM, Godmar Back <[log in to unmask]> wrote:
> >
> > > 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
> > > >
> > >
> >
> >
> >
> > --
> > Nate Hill
> > [log in to unmask]
> > http://www.natehill.net
> >
>
--
Nate Hill
[log in to unmask]
http://www.natehill.net
|