问题描述
我正在
@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 上是否存在视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!