0

I am trying to get the response json of an oEmbed request. Heres the code:

var _url = 'http://www.youtube.com/oembed?url=http%3A//youtube.com/watch%3Fv%3DM3r2XDceM6A&format=json';

$.ajax({
    url: _url,
    type: 'GET',
    dataType: 'jsonp',
    crossDomain: true,
    success: function(){
        alert(this.responseText);
    },
    error: function(){
        alert('error');
    }
});

The console throws the error:

[Error] SyntaxError: Unexpected token ':'. Parse error. (anonymous function) (oembed, line 1)

Update

I have corrected the dataType property to json. That seems to have solved that problem. Now I am getting an Access-Control-Allow-Origin error.

Tom Taylor
  • 3,344
  • 2
  • 38
  • 63
val
  • 729
  • 6
  • 19
  • 1
    It's because the URL is returning JSON, not JSONP. Set `dataType: 'json'`. Also note that `this.responseText` will not work, use the parameter provided to the `success` function to retrieve the data in the response. This is of course assuming you're not prevented from retrieving the data due to the Same Origin Policy – Rory McCrossan Feb 23 '16 at 11:55
  • That seems to have solved that problem. Now Iam getting an Access-Control-Allow-Origin error. – val Feb 23 '16 at 11:57
  • That means you can't make the request to that endpoint via JS alone. There's nothing you can do about that one. See the question I marked as a duplicate for more information. – Rory McCrossan Feb 23 '16 at 11:57
  • Please refer https://stackoverflow.com/questions/12929249/oembed-for-youtube/44893308#44893308 - you may find it useful.. – Tom Taylor Jul 03 '17 at 21:44

0 Answers0