Embed youtube videos using oembed

强颜欢笑 提交于 2019-12-05 09:26:05

Actually the problem is you're violating the browser same origin policy with a cross domain ajax request. There a few work potential work arounds -- unfortunately the best JSONP, isn't implemented by YouTube. The next best is using Flash for transport. This is used by YUI-IO utility. Also you can see Jquery suggestions here.

I get the json data just fine if I embed the raw trololo url in the oembed url. I'm guessing that by typing in the encoded version into the address bar does a layer of decoding anyways, so try just sending the raw one:

http://youtube.com/oembed?url=http://www.youtube.com/watch?v=iwGFalTRHDA&format=json

Use the json-c version: https://developers.google.com/youtube/2.0/developers_guide_jsonc

    var id = "iwGFalTRHDA";
    $.ajax({
        url: "https://gdata.youtube.com/feeds/api/videos/" + id + "?v=2&alt=jsonc",
        dataType: "jsonp",
        success: function (data) {
            console.log(data);
        }
    });

I had a similar problem, turns out that the url query string parameter was using the www.youtube.com domain, whereas my call to the oembed endpoint was using youtube.com/oembed. Using www.youtube.com/oembed fixed the problem.

Got the same problem. I have "solved" this by having a url in my server download the JSON and then send it the client.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!