我正在尝试获取一些直播的在线状态和观众人数。我通过 AJAX 从我的网站获取对象数组。然后我遍历数组并向这些对象添加值。问题是,当我在控制台中记录这些对象的内容时,我找到了新创建的键,但是当我尝试记录该键的值时,它说它是未定义的。
$.each(livestreams, function () {
if(this.provider === 'TwitchTV') {
$.ajax({
url: 'http://api.justin.tv/api/stream/list.json?channel=' + this.channel.toLowerCase(),
dataType: 'jsonp',
jsonp: 'jsonp',
success: $.proxy(function (stream) {
this.live = true;
this.count = stream[0].channel_count;
}, this)
});
} else {
$.ajax({
url: 'http://api.own3d.tv/liveCheck.php?live_id=' + this.channel,
dataType: 'xml',
success: $.proxy(function (stream) {
this.live = stream.find('isLive').text();
this.count = stream.find('liveViewers').text();
}, this)
});
}
console.log(this);
/* returns
channel: "garenatw"
count: 8237
id: "3"
live: true
name: "garena"
provider: "TwitchTV"
username: "grifon"
__proto__: Object
(of course, only for this specific object)
*/
console.log(this.live); // returns undefined
});