JSONP request in AngularJS doesn't work like in jQuery

浪尽此生 提交于 2019-12-12 21:47:58

问题


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 : 

回答1:


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.



来源:https://stackoverflow.com/questions/19269153/jsonp-request-in-angularjs-doesnt-work-like-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!