gpt4 book ai didi

json - 发生错误,API SoundCloud自定义播放器

转载 作者:行者123 更新时间:2023-12-03 08:58:05 25 4
gpt4 key购买 nike

嗨,我试图在发生错误时捕获错误,但我对Json有问题
当轨道完美加载时,我收到了成功的答案,但是在坠机中却一无所有

$.getJSON(apiUrl, function(data, error) {
index += 1;
if(data.tracks){
playerObj.tracks = playerObj.tracks.concat(data.tracks);
}else if(data.duration){
data.permalink_url = link.url;
playerObj.tracks.push(data);
}else if(data.creator){
links.push({url:data.uri + '/tracks'});
}else if(data.username){
if(/favorites/.test(link.url)){
links.push({url:data.uri + '/favorites'});
}else{
links.push({url:data.uri + '/tracks'});
}
}else if($.isArray(data)){
playerObj.tracks = playerObj.tracks.concat(data);
}
if(links[index] && (index % 18) != 0){
var mod = index % 18;
loadUrl(links[index]);
}else{
playerObj.node.trigger({type:'onTrackDataLoaded', playerObj: playerObj, url: apiUrl});
if (links[index]) {
loadMoreTracksData($player, links, key, index);
}
}
}).success(function() { console.log("second success"); }).error(function() { console.log("error"); });

有谁知道如何正确收听此Api中的错误。

最佳答案

假设您使用的是jQuery> 1.5版本,则下面的结构会更适合。

仅当从服务器返回适用的HTTP代码(例如500/404/400)时,不仅调用返回空数据集时,还会成功执行HTTP 200响应时,才会调用fail方法。

var index = 0;

var processResponse = function(data) {
index += 1;
if(data.tracks){
playerObj.tracks = playerObj.tracks.concat(data.tracks);
}else if(data.duration){
data.permalink_url = link.url;
playerObj.tracks.push(data);
}else if(data.creator){
links.push({url:data.uri + '/tracks'});
}else if(data.username){
if(/favorites/.test(link.url)){
links.push({url:data.uri + '/favorites'});
}else{
links.push({url:data.uri + '/tracks'});
}
}else if($.isArray(data)){
playerObj.tracks = playerObj.tracks.concat(data);
}
if(links[index] && (index % 18) != 0){
var mod = index % 18;
loadUrl(links[index]);
}else{
playerObj.node.trigger({type:'onTrackDataLoaded', playerObj: playerObj, url: apiUrl});
if (links[index]) {
loadMoreTracksData($player, links, key, index);
}
}

var processError = function(error){
console.log("Error:" + error);
}

$.getJSON(apiUrl)
.done(function(data){ processResponse(data); })
.fail(function(error){ processError(error); })
.complete(function(xhr, status) {console.log(status);};

关于json - 发生错误,API SoundCloud自定义播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17272941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com