Vimeo ID validity with jQuery

2019-05-31 04:24发布

问题:

I know how to get the id of a video, but I want to check its validity too. If a video ID is good, there's the alert with "good ID", but when the url is wrong it fails. I've tried try-catch, fail, error, nothing worked. Is there any idea? :(

var video_id  = '29994384';
var video_url = 'http://vimeo.com/api/v2/video/' + video_id + '.json';

$.ajax({
    type: 'GET',
    url: video_url,
    data: {format: 'jsonp'},
    dataType: 'jsonp',
    crossDomain: true,
    success: function(resp) {
        alert('good ID');
    },
    error: function(resp) {
        alert('wrong ID');
    }
});

回答1:

You can check other properties coming from response if they exists and if so, it is valid something like this:

success: function(resp) {
   if (resp['title']) {
      alert ('good ID');
   }
   else {
      alert ('bad ID');
   }
},