我可能在这里遗漏了一些东西,但我无法使这个JSONP请求工作,这里是代码:
var url = 'http://' + server + '?callback=JSON_CALLBACK';
$http.jsonp(url)
.success(function(data){
console.log('success');
})
.error(function () {
console.log('error')
});
Run Code Online (Sandbox Code Playgroud)
该请求触发正常,我以这种格式获得响应(带有标题Content-Type:application/json):
[{"id": "1", "name": "John Doe"},
{"id": "2", "name": "Lorem ipsum"},
{"id": "3", "name": "Lorem ipsum"}]
Run Code Online (Sandbox Code Playgroud)
你能看到什么问题吗?也许我应该从服务器返回的格式不对?Angular会触发错误回调,除了我设置的错误消息之外没有任何错误消息('错误').
我无法让NYTimes API正确返回JSON.我确信这是对我们做错的事情.我尝试使用ESPN API,它工作正常.不确定我错过了什么.这是一个代码.
app.controller('espn', function($scope,$http){
//var url = "http://api.espn.com/v1/sports/news/headlines/top?limit=9&apikey=n39jrj4s97tvhxym4qgacnrd&callback=JSON_CALLBACK";
var url = "http://api.nytimes.com/svc/news/v3/content/all/all/.json?&limit=20&api-key=1f6ef65ff5bb290bdcb01da786c788de:2:67858849&callback=JSON_CALLBACK";
$http.jsonp(url)
.success( function(data){
console.log(data);
});
});
Run Code Online (Sandbox Code Playgroud)
我在错误控制台中收到此错误. 未捕获的SyntaxError:意外的令牌:
这是掠夺者.Plunker
我想用AngularJS的$ http服务请求Dailymotion API,但它似乎不像jQuery那样工作.
var url = 'https://api.dailymotion.com/videos?fields=id,audience,onair&ids=xzttq2_c,xrw2w0';
Run Code Online (Sandbox Code Playgroud)
jQuery.ajax({
type: 'GET',
url: url,
dataType: 'jsonp',
success: function(data) {
console.log('Success', data);
},
error: function(data) {
console.log('Error', data);
}
});
Run Code Online (Sandbox Code Playgroud)
使用jQuery,它工作正常.我不必使用回调.
Success
Object {page: 1, limit: 10, explicit: false, has_more: false, list: Array[2]}
Run Code Online (Sandbox Code Playgroud)
$http({
method: 'jsonp',
url: url,
responseType: "json"
}).
success(function (data) {
console.log('Success', data);
}).
error(function (data) {
console.log('Error', data);
});
Run Code Online (Sandbox Code Playgroud)
但是对于Angularjs,它并没有像我预期的那样工作.
Uncaught SyntaxError: Unexpected token :
Run Code Online (Sandbox Code Playgroud)