【问题标题】:How do I check if a video exists on YouTube, in client side如何在客户端检查 YouTube 上是否存在视频
【发布时间】:2015-05-02 18:17:30
【问题描述】:

我正在做validation for my Youtube url text field

我需要检查,如果Youtube url 不存在我应该抛出错误,我按照这个answer 并创建了jsfiddle 来检查它。

它适用于有效的网址,但不适用于无效的网址。我看到的只是network console 中的 404 错误

有没有办法使用JavaScriptjQuery 来检查客户端是否存在url。

这是我的代码:

var videoID = 'kn8yzJITdvI';//not working 
//var videoID = 'p4kIwWHP8Vc';//working 
$.ajax({
    url: "https://gdata.youtube.com/feeds/api/videos/" + videoID + "?v=2&alt=json",
    dataType: "jsonp",
    success: function(data) {
        console.log(data)
          $("#result").text(data);
    },
    error: function(jqXHR, textStatus, errorThrown)
                    {
                        // Handle errors here
                        alert('ERRORS: ' + textStatus);

                    }
});

JSfiddle Link

【问题讨论】:

  • 仍然停留在 youtube 上:P
  • @hitesh 请标记为对您有用的解决方案的答案,以便其他人也可以从中受益
  • @JitendraPancholi : 哈哈 :P yaaa ;)
  • @innomanik : 对不起,下班后我没在线...我会接受正确答案:)

标签: javascript jquery ajax youtube youtube-api


【解决方案1】:

@hitesh,请从 ajax 请求中删除 datatype:'jsonp'。这样,如果视频 id 可用,您将获得 json 字符串,如果视频 id 不可用,则将调用 ajax 错误回调。我试过你的小提琴及其工作。试试这样-

//var videoID = 'kn8yzJITdvI';//not working 
var videoID = 'p4kIwWHP8Vc';//working 
$.ajax({
    url: "https://gdata.youtube.com/feeds/api/videos/" + videoID + "?v=2&alt=json",
    //dataType: "jsonp",
    success: function(data) {
        console.log(data)
          $("#result").text(data);
    },
    error: function(jqXHR, textStatus, errorThrown)
                    {
                        // Handle errors here
                        alert('ERRORS: ' + textStatus);
                    }
});

这是您需要的解决方案的另一个简短实现-

//var videoID = 'kn8yzJITdvI';//not working 
var videoID = 'p4kIwWHP8Vc';//working 

$.getJSON('http://gdata.youtube.com/feeds/api/videos/'+videoID+'?v=2&alt=jsonc',function(data,status,xhr){
    alert(data.data.title);
}).error(function() { alert("error"); });

【讨论】:

  • 感谢您的回答:)
  • 如何在 v3 中做到这一点?似乎该呼叫现在已弃用。我得到: {"apiVersion":"2.1","error":{"code":410,"message":"不再可用","errors":[{"domain":"GData","代码":"NoLongerAvailableException","internalReason":"不再可用"}]}}
  • @mila 看我的回答here
【解决方案2】:

对于那些正在寻找使用 V3 API 的解决方案的人,您可以执行以下操作:

var videoID = 'the_youtube_video_id';
$.getJSON('https://www.googleapis.com/youtube/v3/videos?id=' + videoID 
           + "&key=INSERT_YOUR_API_KEY_HERE&part=COMMA_DELIMITED_VALUE", 
  function (data, status, xhr) {               
    if (data.items.length > 0)
        alert('It is there!')
    else
        alert('Are you sure you about the Id?');

    console.log(status);
    console.log(xhr);
}).error(function (xhr, errorType, exception) {
    var errorMessage = exception || xhr.statusText || xhr.responseText;
    alert(errorMessage);
});

有关有效parts 的列表,您可以访问the doc page here

【讨论】:

    【解决方案3】:
    【解决方案4】:

    来自$.ajax docs

    错误

    注意:跨域脚本和跨域 JSONP 请求不会调用此处理程序。

    【讨论】:

      【解决方案5】:

      已经有人遇到了和你一样的问题,你在做跨域请求时无法检查 404 错误。您应该通过超时来处理它。

      JSONP request error handling

      【讨论】:

        【解决方案6】:

        在客户端试试这个:

        //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
                }
            }
        });
        

        【讨论】:

          猜你喜欢
          • 2015-12-27
          • 2014-07-01
          • 2016-02-26
          • 2010-11-25
          • 2020-12-27
          • 2012-11-10
          • 1970-01-01
          • 1970-01-01
          • 2021-10-19
          相关资源
          最近更新 更多