从twitch api得到回应

时间:2018-04-18 10:24:08

标签: javascript ajax

这个问题对我来说很奇怪。似乎函数getChannels应该返回json,但我得到了未定义。我在return语句之前把log函数放在了正确的位置,它向我显示数据存在。

请看一下我的笔 - https://codepen.io/blooblyboo/pen/VXgdXx

在getChannels()之上,我用给定的名称调用该函数。

$(this).addClass('current');
  if($(this).find('span.all').length > 0){//output channels to corresponding list
    var ul = $(this).find('ul');
    var list = JSON.parse(localStorage.getItem('channels')), len = list.length, channels = [];
      while(len != 0){
        var channel = getChannels(list[--len]);
        // console.log(channel)
      }
  }
});

function getChannels(channelName){//take list from storage and request all of it's items from twitch
  function Channel(name, display_name, bio, logo, _id){
    this.name = name;
    this.display_name = display_name;
    this.bio = bio;
    this.logo = logo;
    this._id = _id;
  }
    var url = 'https://api.twitch.tv/kraken/users/'+channelName+'?client_id=v1oygovy27vh6tml1vb6edbtlsehkx', result;
    $.ajax({
       url: url,
       data: {
          format: 'json'
       },
       error: function() {
          console.log('Couldn\'t get any');
       },
       success: function(data) {
         var channel = new Channel(data.display_name, data.bio, data.logo, data.name, data._id);
         console.log(channel);
         return channel;
       }
    });
}

0 个答案:

没有答案