/* * Get Metadata from Douban api * Created by idalin */ $(document).ready(function () { var get_meta_btn = '
  • ' + '' + '获取Meta
  • '; $('#main-nav').prepend(get_meta_btn); var douban = 'https://api.douban.com'; var search = '/v2/book/search'; var get_info = '/v2/book/'; var get_info_by_isbn = '/v2/book/isbn/ '; $.ajaxSetup({ type: "GET", dataType: "jsonp", jsonp: 'callback', async: false }); get_meta = function (id) { var url = douban + get_info + id; console.log('getting book meta:' + id); $.ajax({ url: url, success: function (meta) { console.log(meta); //$('#metaModal').modal('hide'); $('#description').val(meta.summary); $('#bookAuthor').val(meta.author.join(' & ')); $('#book_title').val(meta.title); var tags = ''; for (var i = 0; i < meta.tags.length; i++) { tags = tags + meta.tags[i].title + ','; } $('#tags').val(tags); $('#rating').val(Math.round(meta.rating.average / 4)); } }); } get_meta_by_isbn = function (isbn) { var url = douban + get_info_by_isbn + isbn; } search_book = function (title) { var url = douban + search + '?q=' + title + '&fields=id,title,author,publisher,isbn13,image,summary'; $.ajax({ url: url, success: function (data) { if (data.books.length < 1) { $('#meta-info').html('

    搜索不到对应的书籍

    '); } else { $('#meta-info').html(''); for (var i = 0; i < data.books.length; i++) { var book = '
  • ' + 'Cover' + '
    ' + '

    ' + data.books[i].title + '

    ' + '

    作者:' + data.books[i].author + '

    ' + '

    出版社:' + data.books[i].publisher + '

    ' + '

    简介:' + data.books[i].summary + '

    ' + '
    ' + '
  • '; $("#book-list").append(book); } } }, error: function () { $('#meta-info').html('

    搜索出错

    '); } }); } $('#get_meta').click(function () { var book_title = $('#book_title').val(); if (book_title) { // console.log(book_title); search_book(book_title); } }); });