问题
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