简体   繁体   English

如何使用JSONP,Ajax和jquery读取数据?

[英]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. 我正在尝试从此API读取数据,但无法正常工作,我有一个输入框,在其中输入isbn编号,然后使用jsonp获取数据。 Could you possibly help me in identifying where my error("Cannot read property 'title' of undefined") is? 您能否帮助我确定我的错误(“无法读取未定义的属性'title'的属性”)在哪里?

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: Open Library的API希望使用bibkeys作为类型和冒号的前缀,而不仅仅是数字:

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

The colon also means the value should be URL-encoded, which jQuery can do for you: 冒号还表示该值应该是URL编码的,jQuery可以为您做到这一点:

$.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: 然后,它返回的数据将重复使用您作为属性提供的bibkeys

{ "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/ 示例: https//jsfiddle.net/3p6s7051/


You can also retrieve the bibkey from the object itself using Object.keys() : 您还可以使用Object.keys()从对象本身检索bibkey

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/ 示例: 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;
             ....
       }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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