6

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?

Andy E
  • 338,112
  • 86
  • 474
  • 445
Dhwanit
  • 153
  • 1
  • 4
  • 10
  • 1
    The url http://www.youtube.com/oembed?callback=jsonp1282594404887&_=1282594622137&format=json&url=http://www.youtube.com/watch%3Fv%3DslORb622ZI8 doesn't return JSON with padding, it just returns JSON. Are you sure the oEmbed API supports JSONP? – Andy E Aug 23 '10 at 20:23
  • i believe the problem is that, the youtube.com/oembed?.... returns only json and it doesnot include the part where a function should be called on jsonp return. Is there a solution where the response is treated as text and not json? If i use dataType in jquery.ajax() to be xml or text, the request fails as ajax request cannot be made to an external domain – Dhwanit Aug 23 '10 at 21:53

2 Answers2

3

Problem

YouTube API doesn't support JSONP - see:

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:

rsp
  • 107,747
  • 29
  • 201
  • 177
0

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.

polarblau
  • 17,649
  • 7
  • 63
  • 84