2

I would like to request the Dailymotion API with the $http service of AngularJS, but it doesn't seem to work like in jQuery.

var url = 'https://api.dailymotion.com/videos?fields=id,audience,onair&ids=xzttq2_c,xrw2w0';

Request using jQuery

jQuery.ajax({
  type: 'GET',
  url: url,
  dataType: 'jsonp',
  success: function(data) {
    console.log('Success', data);
  },
  error: function(data) {
    console.log('Error', data);
  }
});

Result

With jQuery, it's work fine. I don't have to use a callback.

Success 
Object {page: 1, limit: 10, explicit: false, has_more: false, list: Array[2]}

Request using AngularJS

$http({
    method: 'jsonp',
    url: url,
    responseType: "json"
}).
success(function (data) {
    console.log('Success', data);
}).
error(function (data) {
    console.log('Error', data);
});

Result

But with Angularjs, it doesn't work as I expected.

Uncaught SyntaxError: Unexpected token : 
JMaylin
  • 1,378
  • 4
  • 15
  • 38

1 Answers1

7

Add this to your url : &callback=JSON_CALLBACK.

Working: http://jsfiddle.net/dqcpa/

From jQuery' ajax method documentation:

"jsonp": Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to specify the callback.

Ivan Chernykh
  • 41,617
  • 13
  • 134
  • 146
  • My question was more: Why don't you need to add a callback in jQuery? But I guess jQuery just implicitly adds `&callback=JSON_CALLBACK`. Thank you Cherniv, I'm going to do that. – JMaylin Oct 09 '13 at 10:37
  • @JMaylin Yes , this is exactly what jQuery is doing! (http://api.jquery.com/jQuery.ajax/) – Ivan Chernykh Oct 09 '13 at 10:43