0

Hi I'm trying to catch an Error when it occurs but I have a problem with Json When the track is loading perfectly I received a success answer but in a crash I have nothing

$.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"); });

Does anyone know how to listen correctly Errors in this Api.

hafid
  • 11
  • 1
  • This is any soundcloud api url with a mistake like var url = 'https://api.oundcloud.com/tracks.json?client_id=YOUR_CLIENT_ID'; Here you have soundcloud without 'S' or when the song doesn't more exist – hafid Jun 25 '13 at 09:06
  • well, it's not really anything to do with soundcloud's api then is it? Should be tagged `oundcloud` :p – nickf Jun 27 '13 at 22:18
  • Lol of course it must be tagged soundcloud api because I found anything in there doc about JSON error occurs when they use it. – hafid Jul 02 '13 at 10:36

1 Answers1

0

Assuming you're using a version of jQuery > 1.5 the below is a structure that would be better suited.

The fail method will only be called when an applicable HTTP code is returned from the server (e.g 500 / 404 / 400) not simply when an empty set of data is returned, but with a successful HTTP 200 response.

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);};
Sparko
  • 735
  • 6
  • 15
  • I edit my code but its still not working. I understand the return of HTTP status code but i thing this return is faster than something I'm a novice in Json but when an Error occurs anything go in the method .done(). – hafid Jun 24 '13 at 16:11
  • So the response is a HTTP 200, but the JSON object is either representing the error within the JSON, or possibly a timeout is occurring. I've updated my answer with the complete method which may provide you further clues as to what error is happening. – Sparko Jun 24 '13 at 16:36
  • Is it possible the jquery catch the HTTP status before the JSON object ? Because I see in my console a "GET 404 not found" from jquery . – hafid Jun 24 '13 at 21:25
  • Do you mean you see in the FireBug-Net console hat a 404 is getting returned, or that the above jQuery console.log statement has reported the "GET 404 not found". If the latter then it's already doing what you're after (i.e not trying to deal with the JSON object). – Sparko Jun 25 '13 at 02:55
  • I found a solution but I think is not correct. This is [that](http://www.haykranen.nl/2011/02/25/jquery-1-5-and-jsonp-requests/). They put a timeout to considered the return of JSON failed. – hafid Jun 25 '13 at 08:58
  • in theory if I follow the [JQUERY's documentation](http://api.jquery.com/jQuery.getJSON/) I have to put your code – hafid Jun 25 '13 at 09:05