2016-07-27 50 views
0

嗨大家好我使用谷歌de api的书籍来阅读关于书籍的信息我打电话与URL号码的URL,这返回一个JSON我创建一个散列,我保存的数据,我想。在其他varibale保存ajax的请求

下一步如果得到这个散列值来保存在数据库..任何想法如何获得这个值,并走出去。 这是我的代码

  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

任何想法!

在此先感谢

+0

为什么你用'id = void 0'?这是没用的任何声明包含'void 0' –

+0

什么是break;'为什么? –

回答

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; 
+0

嗨@Shubham Khatri我尝试你的解决方案,我得到这个错误,Uncaught ReferenceError:请求没有被定义,任何想法! – Stone

+0

@Stone重命名request_element在所有这三个地方,并看到 –

+0

谢谢@Shubham Khatri我这样做,但我仍然有同样的错误,无论变量的名称 – Stone

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