Kar*_*ten 6 javascript youtube jquery json oembed
我想使用oembed从youtube链接获取jQuery的嵌入代码:
var url = "http://www.youtube.com/watch?v=iwGFalTRHDA";
url = encodeURIComponent(url);
$.getJSON('http://youtube.com/oembed?url='+url+'&format=json', function(data) {
console.log(data);
});
Run Code Online (Sandbox Code Playgroud)
好吧,我没有得到任何数据.
有趣的是,如果我浏览到网址,我会得到正确的回复:
http://www.youtube.com/oembed?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DiwGFalTRHDA&format=json`
Run Code Online (Sandbox Code Playgroud)
引导我
{
provider_url: "http://www.youtube.com/"
title: "Trololo"
html: "<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/iwGFalTRHDA?version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/iwGFalTRHDA?version=3" type="application/x-shockwave-flash" width="425" height="344" allowscriptaccess="always" allowfullscreen="true"></embed></object>"
author_name: "KamoKatt"
height: 344
thumbnail_width: 480
width: 425
version: "1.0"
author_url: "http://www.youtube.com/user/KamoKatt"
provider_name: "YouTube"
thumbnail_url: "http://i2.ytimg.com/vi/iwGFalTRHDA/hqdefault.jpg"
type: "video"
thumbnail_height: 360
}
Run Code Online (Sandbox Code Playgroud)
我还使用了jquery oembed插件,但是如果请求成功,也会抛出onError选项.
我真的很期待一些想法......
实际上问题是你违反了浏览器相同的源策略与跨域ajax请求.有一些工作潜在的工作 - 不幸的是最好的JSONP,并没有由YouTube实施.接下来最好的是使用Flash进行传输.这由YUI-IO实用程序使用.你也可以在这里看到Jquery的建议.