sholsapp
sholsapp

Reputation: 16070

I want to request JSON inside of a WordPress page

For the last few hours I've been trying to set up this http://code.google.com/apis/books/docs/dynamic-links.html on a WordPress blog. Google's API sends back a JSON response (which is supposed to be "put" into _GBSBookInfo variable). However, that variable never is assigned so my javascript callback function explodes saying the variable doesn't exist. So far, all of my javascript is in the WordPress header.

I tried this outside of WordPress and it works fine.

This is the static page:

<script src="http://books.google.com/books?bibkeys=0307346609&jscmd=viewapi&callback=response_handler">

This is the handler:

function response_handler(data) { 
   var bookInfo = _GBSBookInfo["0307346609"]; // the var that doesn't exist
   document.getElementById("test123").innerHTML = bookInfo.thumbnail_url;
} 

Thanks for any help in advance, WordPress has been extremely frustrating by limiting so much! If I'm doing anything stupid please say so, I'm a new javascript programmer.

EDIT:

I've used firebug so far to identify the problem to be: the _GBSBookInfo variable never gets "created" or "exists". I'm not sure how javascript works at this level. Hopefully this helps.

ERRORS:

Error: _GBSBookInfo is not defined Line: 79

Upvotes: 0

Views: 215

Answers (1)

bschaeffer
bschaeffer

Reputation: 2904

Try replacing _GSBookInfo with data, like so:

function response_handler (data) {
    var bookInfo = data["0307346609"];
    document.getElementById("test123").innerHTML = bookInfo.thumbnail_url;
}

Based on your post, google returns this:

response_handler({
    "0307346609": {
        "bib_key":"0307346609",
        ....
        "thumbnail_url":"http://bks2.books.google.com/books?somethumbnailstuff"
    }
});

... so the above code should work for you.

Upvotes: 1

Related Questions