将Oembed与Hulu结合使用(并理解JSONp)

Using Oembed with Hulu (and understanding JSONp)

本文关键字:JSONp Oembed Hulu 结合      更新时间:2023-09-26

如果有人能阐明这个过程中的任何一个,那就太好了。这是交易。考虑到Hulu视频URL(例如"www.Hulu.com/watch/154344"),我希望能够使用Javascript(w/jQuery很好)来检索嵌入URL(即"http://www.hulu.com/embed.html?eid=wvqxnyjrtho0a6osltsuig’)。

此外,正如您所知,我正在localhost上的开发环境中工作。

在我看来,最简单的方法是使用oembed服务。Hulu提供oembed服务,这在ombed页面上进行了演示。纯粹基于oembed页面上显示的示例,此eombed信息的url格式应为http://www.hulu.com/api/oembed.xml?url=http%3A//www.hulu.com/watch/154344"。如果我在浏览器中打开它,那就行了!

所以我试着运行这样的Javascript:

var url = 'http://www.hulu.com/api/oembed.xml?url=http%3A//www.hulu.com/watch/154344';
$.getJSON(url, function(data) {
    alert(data.embed_url);
});

但是,这不起作用。我收到一个错误,上面写着:

XMLHttpRequest cannot load http://noembed.com/embed?url=http%3A//www.hulu.com/watch/154344. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

所以我又查了一下。看起来我需要使用JSONp。我还阅读了更多关于JSONp的内容。要启用JSONp,我所需要做的就是将URL格式更改为这样,在末尾添加callback=data:http://www.hulu.com/api/oembed.xml?url=http%3A//www.hulu.com/watch/154344&callback=数据。没有骰子。它仍然给我同样的错误。

我做错了什么?

在jQuery中使用JSONP时,不要给回调指定名称。使用?,jQuery会将其替换为您的名称。

var url = 'http://www.hulu.com/api/oembed.xml?url=http%3A//www.hulu.com/watch/154344&callb‌​ack=?';