使用Oembed与Hulu(并理解JSONp)

时间:2014-04-21 19:23:22

标签: javascript jquery json jsonp oembed

如果有人能够对任何一个过程有所了解,那就太好了。这是合约。鉴于Hulu视频网址(例如' www.hulu.com/watch/154344'),我希望能够使用Javascript(w / jQuery很好)来检索嵌入网址(即&#39 ; http://www.hulu.com/embed.html?eid=wvqxnyjrtho0a6osltsuig'。)

另外,您知道,我正在localhost上的开发环境中工作。

在我看来,最简单的方法是使用oembed服务。 Hulu提供了oembed服务,该服务已经展示on the ombed page。完全基于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。我也读过a bit more about JSONp。看起来我需要做的就是启用JSONp,将URL格式更改为此,将callback=data添加到结尾:http://www.hulu.com/api/oembed.xml?url=http%3A//www.hulu.com/watch/154344&callback=data。没有骰子。它仍然给我同样的错误。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

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

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