在其他varibale中保存ajax的请求

时间:2016-07-27 17:31:15

标签: javascript php ajax

大家好我正在使用Google的de api书来阅读有关书籍的信息我称之为带有ISBN编号的网址,这将返回一个JSON我创建了一个哈希,我保存了我想要的数据。

下一步如果获取此哈希值以保存在数据库中..任何想法如何获取此值并退出。 这是我的代码

            var id = "0716604892";
            var post_url = "https://www.googleapis.com/books/v1/volumes?q=isbn:";
            post_url = post_url + id;

            findBook("0716604892");

          function findBook(elem) {
            var id, post_url, request;
            post_url = "https://www.googleapis.com/books/v1/volumes?q=isbn:";
            id = elem;
            post_url = post_url + id;
            request = function (){
                $.ajax({
              url: post_url,
              method: 'GET',
              data: {
                dataType: "json"
              },
              success: function(html, data) {
                var author, hashPetName, image, title;
                title = html['items'][1]['volumeInfo']['title'];
                author = html['items'][1]['volumeInfo']['authors'][0];
                image = html['items'][1]['volumeInfo']['imageLinks']['smallThumbnail'];
                hashPetName = {
                  'title': title,
                  'author': author,
                  'image': image
                };
              },
              error: function(errorThrown) {
                console.log(errorThrown);
              }
            });
            return hasBookName
          };
        };
          elemento_save = request;

我想要elemento_save = hasBookName

任何想法!

提前致谢

2 个答案:

答案 0 :(得分:0)

此外,如果您希望request保存匿名函数的结果,则需要进行函数调用:

function findBook(elem) {
        var id, post_url, request;
        id = void 0;
        post_url = void 0;
        post_url = "https://www.googleapis.com/books/v1/volumes?q=isbn:";
        id = elem;
        var hasBookName = null;
        post_url = post_url + id;
        request = function (){
          $.ajax({
          url: post_url,
          method: 'GET',
          data: {
            dataType: "json"
          },
          success: function(html, data) {
            var author, hashPetName, image, title;
            title = html['items'][1]['volumeInfo']['title'];
            author = html['items'][1]['volumeInfo']['authors'][0];
            image = html['items'][1]['volumeInfo']['imageLinks']['smallThumbnail'];
            hasBookName = {
              'title': title,
              'author': author,
              'image': image
            };

          },
          error: function(errorThrown) {
            console.log(errorThrown);
          }
        })
        return hasBookName
       }();
    element_save = request;

答案 1 :(得分:0)

最后我找到了解决方案,请参阅coffescript中的代码

salida = findBook("0716604892")
alert(salida['title'])



findBook = (elem) ->
  id = undefined
  post_url = undefined
  request_element = undefined
  post_url = 'https://www.googleapis.com/books/v1/volumes?q=isbn:'
  id = elem
  post_url = post_url + id
  result = ''
  $.ajax
    url: post_url
    async: false
    success: (data) ->
      author = undefined
      hashPetName = undefined
      image = undefined
      title = undefined
      title = data['items'][1]['volumeInfo']['title']
      author = data['items'][1]['volumeInfo']['authors'][0]
      image = data['items'][1]['volumeInfo']['imageLinks']['smallThumbnail']
      hashPetName =
        'title': title
        'author': author
        'image': image
      result = hashPetName
      return
  result