我有问题,当试图表明我得到的数据。 这是我的code:
$ HTTP({
方法:'得到',
格式:JSON,
url:'http://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=MyAPI&per_page=30&format=json',
}),成功(功能(数据,状态,头,配置){
$ scope.photoID =数据;
警报($ scope.photoID)
})
JSON文件获取:
jsonFlickrApi(
{
照片:{
页面:1,
页数:34,
perpage:30,
总:1000,
照片: [
{
ID:11480313795,
老板:80249365 @ N00,
天机:d4950d1c38
服务器:5482,
农场:6,
标题:5DM38945
ispublic:1,
isfriend:0,
isfamily:0
} .....
而在这种情况下,它可以显示在警报窗口,整个JSON文件。 然而,当我想要得到这样的页面或照片的身份证一些特定的数据,它只是显示在控制台中未定义的,甚至错误。
$ HTTP({
方法:'得到',
数据类型:JSONP,
url:'http://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=MyAPI&per_page=30&format=json&jsoncallback=?',
}),成功(功能(数据,状态,头,配置){
$ scope.photoID = data.photos.page;
警报($ scope.photoID);
})
我可以阿贾克斯与jQuery相同的JSON文件,所以我不知道什么是错的。 任何人都可以帮助我吗?
解决方案正在使用 $ http.get
方法。 Flickr的API,你虽然JSONP格式使用的回报。这是什么,似乎让你的code不起作用。看看我原来的答案正确的方式来处理这种在这种情况下。
previous答:
从 $ HTTP 的文档,您的电话已包含JSON_CALLBACK子。
你的是丢失了。
正确的网址应该是:
http://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=MyAPI&per_page=30&format=json&jsoncallback=JSON_CALLBACK
编辑:
下面是一个plunkr,与一个测试用例
http://plnkr.co/edit/MUMnHML5QtDJTthmtZ2Y?p=$p$ PVIEW
code:
$http.jsonp("http://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=KEY&per_page=30&format=json&jsoncallback=JSON_CALLBACK") .success(功能(数据){ $ scope.data = data.photos; })I have issue when try to show the data I get. Here is my code:
$http({
method:'get',
format:'json',
url:'http://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=MyAPI&per_page=30&format=json',
}).success(function(data, status, headers, config){
$scope.photoID = data;
alert($scope.photoID)
})
The Json file I get:
jsonFlickrApi(
{
photos: {
page: 1,
pages: 34,
perpage: 30,
total: 1000,
photo: [
{
id: "11480313795",
owner: "80249365@N00",
secret: "d4950d1c38",
server: "5482",
farm: 6,
title: "5DM38945",
ispublic: 1,
isfriend: 0,
isfamily: 0
},.....
And in this situation it can show the whole json file in alert window. However when I want to get some specific data like page or photo id,it just shows undefined or even error in the console.
$http({
method:'get',
dataType:'jsonp',
url:'http://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=MyAPI&per_page=30&format=json&jsoncallback=?',
}).success(function(data, status, headers, config){
$scope.photoID = data.photos.page;
alert($scope.photoID);
})
I can ajax the same json file with Jquery,so I have no idea what's wrong. Can anyone help me please?
解决方案You are using the $http.get
method. The flickr api you are using returns a JSONP format though. This is what seems to make your code not work. Check out my original answer for the correct way to handle this in this case.
Previous Answer:
From the $http docs, your call has to contain a JSON_CALLBACK substring.
Yours is missing.
The correct url should be:
http://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=MyAPI&per_page=30&format=json&jsoncallback=JSON_CALLBACK
EDIT:
Here is a plunkr, with a test case
http://plnkr.co/edit/MUMnHML5QtDJTthmtZ2Y?p=preview
Code:
$http.jsonp("http://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=KEY&per_page=30&format=json&jsoncallback=JSON_CALLBACK")
.success(function(data) {
$scope.data = data.photos;
})