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 : 
0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 Yes , this is exactly what jQuery is doing! (api.jquery.com/jQuery.ajax)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.