简体   繁体   中英

How to read data using JSONP, Ajax and jquery?

I am trying to read data from this API, but it is not working, I have an input box where I enter the isbn number and then get the data, using jsonp. Could you possibly help me in identifying where my error("Cannot read property 'title' of undefined") is?

function add(){
        var isbn = parseInt($("#isbn").val());
        var list = $("#list");
        console.log(parseInt(isbn));

        $.ajax({
            url: "https://openlibrary.org/api/books?bibkeys=" + isbn + "&jscmd=details&callback=mycallback",
            dataType: "jsonp",
            success: function(isbn){
                var infoUrl = isbn.info_url;
                var thumbnailUrl = isbn.thumbnail_url;
                var title = isbn.details.title;
                var publishers = isbn.details.publishers;
                var isbn13 = isbn.details.isbn_13;


                console.log(isbn.info_url);
                              }
        });
    }

Open Library's API expects bibkeys to be prefixed with their type and a colon, rather than just the number alone:

function add(){
    var isbn = 'ISBN:' + $("#isbn").val();
    // ...

The colon also means the value should be URL-encoded, which jQuery can do for you:

$.ajax({
    url: "https://openlibrary.org/api/books?jscmd=details&callback=?",
    data: { bidkeys: isbn },
    dataType: "jsonp",

Then, the data it returns reuses the bibkeys you provided as properties:

{ "ISBN:0123456789": { "info_url": ..., "details": { ... }, ... } }

To access the book's information, you'll have to first access this property:

success: function(data){
    var bookInfo = data[isbn];

    console.log(bookInfo.details.title);
    // etc.
}

Example: https://jsfiddle.net/3p6s7051/


You can also retrieve the bibkey from the object itself using Object.keys() :

success: function (data) {
    var bibkey = Object.keys(data)[0];
    var bookInfo = data[bibkey];

    console.log(bookInfo.details.title);
    // ...
}

Note: You can use this to validate, since the request can be technically successful and not include any book information (ie no matches found):

 success: function (data) { var bibkeys = Object.keys(data); if (bibkeys.length === 0) return showError('No books were found with the ISBN provided.'); // ... 

Example: https://jsfiddle.net/q0aqys87/

I asked a professor, and this is how she told me to solve it:

function add(){
    var isbn = parseInt($("#isbn").val());
    var list = $("#list");
    console.log(parseInt(isbn));

    $.ajax({
        url: "https://openlibrary.org/api/books?bibkeys=" + isbn + "&jscmd=details&callback=mycallback",
        dataType: "jsonp",
        success: function(data){
            var thumb=data["ISBN:"+isbn+""].thumbnail_url;
             ....
       }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM