jQuery getJSON not working for deleted twitch channel

核能气质少年 提交于 2019-12-08 12:30:37

问题


I am trying to make a list of twitch channels that shows who is online/offline and show a message of the person has deleted there account. The problem is that nothing works inside the getJSON method if the persons account no longer exists even though the request returns a JSON file with a couple of properties.

Codepen: http://codepen.io/ZacharyKearns/pen/obxREy/

https://api.twitch.tv/kraken/channels/brunofin //deleted channel

{"error":"Unprocessable Entity","status":422,"message":"Channel 'brunofin' is not available on Twitch"}

Here is the code;

var streamers = ["freecodecamp", "medrybw", "brunofin", "storbeck", "terakilobyte", "habathcx", "RobotCaleb", "thomasballinger", "noobs2ninjas", "beohoff"],
 streamerList = $('ul.list-group');

$.each(streamers, function(i) {

 $.getJSON("https://api.twitch.tv/kraken/channels/" + streamers[i], function(channelData) {

  $.getJSON("https://api.twitch.tv/kraken/streams/" + streamers[i], function(streamData) {

   if (channelData.status == 422) {

    var listItem = $('<li/>').addClass('list-group-item').appendTo(streamerList),
     nameLink = $('<a/>').html(channelData.message).appendTo(listItem);

   } else {

    var listItem = $('<li/>').addClass('list-group-item').appendTo(streamerList),
     nameLink = $('<a/>').html(channelData.display_name).appendTo(listItem);

   }
  });
 });
});

回答1:


https://api.twitch.tv/kraken/channels/brunofin comes back with a 422 network error code. It will never enter the 'success-handler' of the $.getJSON.

You should add a .fail() handler on the json call.

$.getJSON("https://api.twitch.tv/kraken/channels/" + streamers[i],  function(channelData) { 

   }).fail(function(err) {
     //handle fail here
   });


来源:https://stackoverflow.com/questions/34458068/jquery-getjson-not-working-for-deleted-twitch-channel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!