0

嗨,我正在尝试在错误发生时捕获错误,但 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 中的错误。

4

1 回答 1

0

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

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

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);};
于 2013-06-24T10:39:23.317 回答