How do I check if a video exists on YouTube, in client side

后端 未结 6 1932
攒了一身酷
攒了一身酷 2021-01-12 16:45

I am doing validation for my Youtube url text field.

I need to check, if the Youtube url does not exist I should throw error, I followed this answer an

6条回答
  •  情深已故
    2021-01-12 16:49

    Try this on client side:

    //here, oABCD01234 is YouTube id
    $.ajax({
        type: 'HEAD',
        url: 'http://gdata.youtube.com/feeds/api/videos/oABCD01234',
        success: function() {
            //it exists!
        },
        error: function(jqXhr) {
            if(jqXhr.status == 400) {
                //it doesn't exist
            }
        }
    });
    

提交回复
热议问题