我试图让所有观众(聊天中的人)从某个频道上抽搐到阵列中。
JSONP调用有效,但Twitch API返回多个数组:
我想把这些数组中的所有用户名放到一个单独的数组中(即“peopleinchat”)。
我的方法是首先声明数组:
var peopleinchat= [];
然后使用$.merge()
将每个后续数组(组)添加到此数组中。我无法让它工作但是...我得到“TypeError:无法读取未定义的属性'长度'”。你能救我一下吗?
谢谢!
$.ajax({
url: "https://tmi.twitch.tv/group/user/batuhanbuyukakkan/chatters",
// The name of the callback parameter, as specified by the YQL service
jsonp: "callback",
// Tell jQuery we're expecting JSONP
dataType: "jsonp",
// Work with the response
success: function( response ) {
var peopleinchat = [];
$.each(response, function(index, data){
$.each(data.chatters, function(index, group){
$('.viewers').append(index + ': ' + group);
$('.viewers').append('<br>');
$.merge(peopleinchat, group);
});
});
$('.array_result').append(peopleinchat);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="viewers"></div>
<br>
<br>
<div class="array_result"></div>
答案 0 :(得分:1)
差不多 - 我认为你的主要问题是你没有处理result
回调success
参数的正确形状。下面的代码片段有效:
$.ajax({
url: "https://tmi.twitch.tv/group/user/batuhanbuyukakkan/chatters",
// The name of the callback parameter, as specified by the YQL service
jsonp: "callback",
// Tell jQuery we're expecting JSONP
dataType: "jsonp",
// Work with the response
success: function( response ) {
var peopleinchat = [];
$.each(response.data.chatters, function(index, data){
$('.viewers').append(index + ': ' + data);
$('.viewers').append('<br>');
$.merge(peopleinchat, data);
});
$('.array_result').append(peopleinchat);
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="viewers"></div>
<br>
<br>
<div class="array_result"></div>
&#13;