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 上是否存在视频
基础教程推荐
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01