jquery - AngularJS 中的 JSONP 请求不像 jQuery 那样工作

标签 jquery angularjs jsonp angularjs-service

我想使用 AngularJS 的 $http 服务请求 Dailymotion API,但它似乎不像 jQuery 那样工作。

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

使用 jQuery 请求

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

结果

使用 jQuery,它工作得很好。我不必使用回调。

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

使用 AngularJS 请求

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

结果

但是对于 Angularjs,它并没有像我预期的那样工作。

Uncaught SyntaxError: Unexpected token : 

最佳答案

将此添加到您的网址:&callback=JSON_CALLBACK

工作中:http://jsfiddle.net/dqcpa/

来自 jQuery 的 ajax 方法 documentation :

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

关于jquery - AngularJS 中的 JSONP 请求不像 jQuery 那样工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19269153/

相关文章:

javascript - 如何解析 JSON 数组

JQUERY JSONP 问题

javascript - AJAX 调用错误 - 状态 400(错误请求)

javascript - 如何让函数调用先执行然后再执行后续代码?

javascript - 在 Angular.js 的 ngModel 中使用表达式

angularjs - 从 Angular Controller 调用 Node 函数的常用方法

javascript - JS 中的迭代、JSON 和回调

jquery - 如何使用 !important 使用 jquery 覆盖内联样式?

javascript - 有效的 JSON 会破坏 HandlebarsJS 和 $.getJSON 调用

javascript - 使用 JavaScript 下载 PDF 文件