1

I am having the following code where realurl = 'TOwd30wXc-0' (youtube video ID):

$.ajax({
        url: "http://gdata.youtube.com/feeds/api/videos/"+realurl+"?v=2&alt=json-in-script",
        dataType: "jsonp",
        contentType: "application/json; charset=utf-8",
        success: function (data) { PreviewVideo(data) }
    });

Where PreviewVideo(data) is the function that parses json response and does other things with it.

My problem is, if video ID is incorrect (doesn't exist), I am not receiving any notice about that. I have tried adding error: function() { alert('error occured') } into $.ajax but it simply does nothing.

Does anybody know a way how to determine if video is okay or not? If video exists, call PreviewVideo(data), else - call VideoError() ?

EDIT: If I put invalid ID, im getting

GET http://gdata.youtube.com/feeds/api/videos/TOwd30wXc-?v=2&alt=json-in-script&callback=jQuery17205096300751902163_1353530502856&_=1353530692237 400 (Bad Request) 

in the javascript console (Chrome)

Thanks in advance

phpDev
  • 23
  • 1
  • 6
  • Can you maybe provide both a valid and invalid value for `realurl` so we can see what's supposed to be returned from the API? – Brad Christie Nov 21 '12 at 20:38
  • How abt if(data != null){PreviewVideo(data)} .. The error function is only called if there was an error with the request or the response.. Otherwise it will hit the success callback – Sushanth -- Nov 21 '12 at 20:40
  • I have tried the following: success: function (data) { if(data != null) { PreviewVideo(data) } else { alert('invalid'); } } but it's not working – phpDev Nov 21 '12 at 20:44

1 Answers1

0

According to the jQuery forums, you cannot catch bad requests. That is to say, statusCode or error parameters won't help (which makes sense given it's really supposed to call a per-determined function name on success).

However, there is a work-around present by using the deferred object interface, example here (Provided by Kevin B on jQuery forums)

I take it back, thought it is solvable using a timeout whih appears to be the best solution on a previous SO question.

Community
  • 1
  • 1
Brad Christie
  • 100,477
  • 16
  • 156
  • 200