开发者

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开发者_如何学Go 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜