问题描述
我正在尝试创建一个抽搐频道列表,以显示谁在线/离线并显示该人已删除该帐户的消息.问题是,即使请求返回的是带有几个属性的JSON文件,即使人员帐户不再存在,getJSON方法中也没有任何作用.
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 //已删除频道
{"error":"Unprocessable Entity","status":422,"message":"Channel 'brunofin' is not available on Twitch"}
这是代码;
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);
}
});
});
});
推荐答案
https://api. twitch.tv/kraken/channels/brunofin 返回了422网络错误代码.它永远不会输入$.getJSON
的成功处理程序".
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
.
您应该在json调用上添加.fail()
处理程序.
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
});
这篇关于jQuery getJSON对于删除的抽搐通道不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!