Search code examples
jqueryangularjsjsonpangularjs-service

JSONP request in AngularJS doesn't work like in jQuery


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 : 

Solution

  • 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.