How do I check if a video exists on YouTube, in client side(如何在客户端检查 YouTube 上是否存在视频)
问题描述
我正在
@hitesh, 请从 ajax 请求中删除 datatype:'jsonp'.这样,如果视频 id 可用,您将获得 json 字符串,如果它不可用,则将调用 ajax 错误回调.我试过你的小提琴及其工作.试试这样-
//var videoID = 'kn8yzJITdvI';//不工作var videoID = 'p4kIwWHP8Vc';//工作$.ajax({url: "https://gdata.youtube.com/feeds/api/videos/" + videoID + "?v=2&alt=json",//数据类型:jsonp",成功:函数(数据){控制台日志(数据)$("#result").text(data);},错误:函数(jqXHR,textStatus,errorThrown){//这里处理错误alert('错误:' + textStatus);}});
这是您需要的解决方案的另一个简短实现-
//var videoID = 'kn8yzJITdvI';//不工作var videoID = 'p4kIwWHP8Vc';//工作$.getJSON('http://gdata.youtube.com/feeds/api/videos/'+videoID+'?v=2&alt=jsonc',function(data,status,xhr){警报(data.data.title);}).error(function() { alert("error"); });
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 and created the jsfiddle to check it.
It works for valid url but it does not work for invalid url. All I see is 404 error in network console
Is there a way to check if url exist in client side using JavaScript
and jQuery
.
here is my code :
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
@hitesh, Please remove the datatype:'jsonp' from the ajax request. This way you'll get json string if the video id is available and if its not available then the ajax error callback would be invoked. I tried on your fiddle and its working. Try like this-
//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);
}
});
Here is another short implementation for the solution you need-
//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"); });
这篇关于如何在客户端检查 YouTube 上是否存在视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在客户端检查 YouTube 上是否存在视频


基础教程推荐
- 每次设置弹出窗口的焦点 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01