【问题标题】:Using getJSON to access Flickr data [duplicate]使用 getJSON 访问 Flickr 数据 [重复]
【发布时间】:2014-06-28 13:42:58
【问题描述】:

我正在尝试访问此用户的 JSON 数据并使用此代码写入此人的 ID:

   <!DOCTYPE html>
<html>
<head>
<script type="text/javascript">

</script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js" type="text/javascript">
API_KEY = 'YOUR_API_KEY';

$.getJSON('https://www.flickr.com/services/rest/?method=flickr.people.getPhotos&api_key=' + API_KEY + '&user_id=22694125@N02&format=json&jsoncallback=?', function(results){
      document.body.innerHTML = JSON.stringify(results.photos.photo[6].id);
  });</script>

</head>
<body>

</body>
</html>

由于某种原因,它不会返回任何东西。帮忙?

【问题讨论】:

  • document.write 会抹掉你的整个页面和代码。
  • @LeeTaylor 页面上没有其他内容,我只想打印照片 ID
  • 将 URL(使用您的 API 密钥)放入浏览器时会发生什么?
  • @LeeTaylor 我明白了:jsonFlickrApi({"photos":{"page":1,"pages":6,"perpage":100,"total":"595","photo ":[{"id":"14150748552","owner":"22694125@N02","secret":"cb6aa07961","server":"7357","farm":8,"title":"union站","ispublic":1,"isfriend":0,"isfamily":0},{"id":"14130209031","owner":"22694125@N02","secret":"ce21e5d60c"," server":"7451","farm":8,"title":"ttc & cntower","ispublic":1,"isfriend":0,"isfamily":0},{"id":"14104453842" “所有者”:“22694125@N02”,“秘密”:“51d7272c69”,“服务器”:“7403”,“农场”:8,“标题”:“scotiabank nuit blanche”,“ispublic”:1, isfriend":0,"isfamily":0},...
  • Learn how todebug JavaScript。我认为请求很好,但您可能无法正确访问响应。

标签: javascript jquery api flickr


【解决方案1】:

根据我读到的内容,我认为您需要这样做:

$.ajax(
{
    url: "https://www.flickr.com/services/rest/?method=flickr.people.getPhotos&api_key=[APIKEY]&user_id=22694125@N02&format=json&jsoncallback=?",
    type: "GET",
    cache: true,
    dataType: 'jsonp',
    success: function(data) 
    {
        console.log(data);
    }
});

我认为调用必须是 JSONP(不是 JSON)。

【讨论】:

  • 你是对的,但如果$.getJSON 在 URL 中检测到 =?,它也会处理 JSONP。见api.jquery.com/jquery.getjson/#jsonp。所以这不太可能有任何区别。
  • 啊,好的。很公平。我没有有效的 API 密钥来测试它。
  • 我也没有...但是我在没有密钥的情况下尝试了它,并且 jQuery 正确替换了 jsoncallback=? 部分并且响应看起来也不错(当然它说我没有有效的 API键)。
  • @user3205630:请求是否已发送?检查开发人员工具中的网络选项卡。检查控制台是否有错误。
  • @user3205630:天啊,我刚刚看到了。您给脚本元素一个src 属性将代码放入其中。那是行不通的。您必须使用两个 script 元素。请看这个 jQuery 教程:learn.jquery.com/about-jquery/how-jquery-works.
猜你喜欢
  • 2020-03-05
  • 2012-09-15
  • 1970-01-01
  • 2014-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-26
相关资源
最近更新 更多