Response for JSONp request to youtube oembed call giving “invalid label” error

你离开我真会死。 提交于 2019-11-27 07:57:20

问题


I am making a JSONp call to youtube using oembed and on response firebug gives "invalid label" error

Here is my code

site = "www.youtube.com";
url = "http://www.youtube.com/watch?v=slORb622ZI8";

$.getJSON("http://"+site+"/oembed?callback=?",{"format":"json","url":url},function(data){
    alert("hello:\n"+data);
    alert(data.provider_url);
});

Anyone ran into similar problem with oembed jsonp requests?


回答1:


Problem

YouTube API doesn't support JSONP - see:

  • Issue 4329: oEmbed callback for JSONP.

Solution

There is no need for a server-side proxy and no API keys are required.

Instead of:

var url = "http://www.youtube.com/watch?v=slORb622ZI8";

$.getJSON("http://www.youtube.com/oembed?callback=?",
    {"format": "json", "url": url}, function (data) {
    alert("hello:\n"+data);
    alert(data.provider_url);
});

Try this, using the Noembed service:

var url = "http://www.youtube.com/watch?v=slORb622ZI8";

$.getJSON("https://noembed.com/embed?callback=?",
    {"format": "json", "url": url}, function (data) {
    alert("hello:\n" + data);
    alert(data.provider_url);
});

As a bonus this will also work with Vimeo links when you change url to:

var url = "https://vimeo.com/45196609";

and many other supported sites.

Demo

See DEMO on JS Fiddle.

See also

See also those questions:

  • Youtube Video title with API v3 without API key?
  • Get Youtube information via JSON for single video (not feed) in Javascript



回答2:


Youtube’s Oembed API doesn’t currently wrap the JSON response in a callback. JSONP is simply not supported atm., and it seems like this won’t change anytime soon: https://groups.google.com/forum/?fromgroups=#!topic/youtube-api-gdata/5KuXxlLK07g

Here’s a ticket for a related feature request: https://code.google.com/p/gdata-issues/issues/detail?id=4329

The easiest solution would be to implement a small server side proxy to make the requests on the client’s behalf.



来源:https://stackoverflow.com/questions/3551286/response-for-jsonp-request-to-youtube-oembed-call-giving-invalid-label-error

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