jQuery.getJSON causes "Uncaught SyntaxError: Unexpected token :" for youtube oembed

Dr. Acula picture Dr. Acula · Jul 3, 2011 · Viewed 10k times · Source

Here is what I'm trying to do:

$.getJSON('http://www.youtube.com/oembed?url=http://www.youtube.com/watch%3Fv%3DB-m6JDYRFvk&callback=?', 
           function(data) { console.log(data) });

When curling that URL I get this response:

{
    "provider_url": "http:\/\/www.youtube.com\/",
    "title": "Coder Girl",
    "html": "\u003cobject width=\"425\" height=\"344\"\u003e\u003cparam name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/B-m6JDYRFvk?version=3\"\u003e\u003c\/param\u003e\u003cparam name=\"allowFullScreen\" value=\"true\"\u003e\u003c\/param\u003e\u003cparam name=\"allowscriptaccess\" value=\"always\"\u003e\u003c\/param\u003e\u003cembed src=\"http:\/\/www.youtube.com\/v\/B-m6JDYRFvk?version=3\" type=\"application\/x-shockwave-flash\" width=\"425\" height=\"344\" allowscriptaccess=\"always\" allowfullscreen=\"true\"\u003e\u003c\/embed\u003e\u003c\/object\u003e",
    "author_name": "dalechase",
    "height": 344,
    "thumbnail_width": 480,
    "width": 425,
    "version": "1.0",
    "author_url": "http:\/\/www.youtube.com\/user\/dalechase",
    "provider_name": "YouTube",
    "thumbnail_url": "http:\/\/i3.ytimg.com\/vi\/B-m6JDYRFvk\/hqdefault.jpg",
    "type": "video",
    "thumbnail_height": 360
}

But when I try to execute the above code, I get an Uncaught SyntaxError: Unexpected token : (Chrome). It looks like the problem might have something to do with the escaping of the forward slashes, or maybe that jQuery is sending a JSONP request, but the response is pure JSON.

Has anyone else run into this problem?

Answer

Nick Craver picture Nick Craver · Jul 3, 2011

Youtube (as of the time of this answer) oembed doesn't support JSONP with their requests, so what you're getting back is correct...but it's not what you need. What you need for JSONP calls would look like this:

functionName({
    "provider_url": "http:\/\/www.youtube.com\/",
    "title": "Coder Girl",
    "html": "\u003cobject width=\"425\" height=\"344\"\u003e\u003cparam name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/B-m6JDYRFvk?version=3\"\u003e\u003c\/param\u003e\u003cparam name=\"allowFullScreen\" value=\"true\"\u003e\u003c\/param\u003e\u003cparam name=\"allowscriptaccess\" value=\"always\"\u003e\u003c\/param\u003e\u003cembed src=\"http:\/\/www.youtube.com\/v\/B-m6JDYRFvk?version=3\" type=\"application\/x-shockwave-flash\" width=\"425\" height=\"344\" allowscriptaccess=\"always\" allowfullscreen=\"true\"\u003e\u003c\/embed\u003e\u003c\/object\u003e",
    "author_name": "dalechase",
    "height": 344,
    "thumbnail_width": 480,
    "width": 425,
    "version": "1.0",
    "author_url": "http:\/\/www.youtube.com\/user\/dalechase",
    "provider_name": "YouTube",
    "thumbnail_url": "http:\/\/i3.ytimg.com\/vi\/B-m6JDYRFvk\/hqdefault.jpg",
    "type": "video",
    "thumbnail_height": 360
});

...since what comes back currently is not valid JavaScript (by itself, and that's what it is), and that's how JSONP works, the response actually needs to be executable JavaScript.

You can get the same error by just plopping that code straight in your page in a <script> block (see a demo here).


I'm not sure exactly what you're trying to do, but if you're just after the embedding piece, I recommend a plugin like jQuery-oembed to do that. If you're after the data...you'll need something on your server to process the JSON, then get the data from your server after that.