我想使用 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/