1

我需要通过标题和响应创建链接从 flickr 获取第一张图片。但我找不到照片元素,我不知道为什么。我的代码:

var options = { 
  "api_key": API-KEY,
  "method": "flickr.photos.search",
  "accuracy": "11",
  "content_type": "1",
  "format": "xmlrpc",
  "per_page": "1",
  "text": TITLE
}

var makeFlickrRequest = function(options, cb) {
    var url, item, first;

    url = "http://api.flickr.com/services/rest/";
    first = true;
    $.each(options, function(key, value) { 
        url += (first ? "?" : "&") + key + "=" + value;
        first = false; 
    });

    $.get(url, function(data) {
        $(data).find('photo').each(function(){
            cb($(this));
        });
    });
};

当我将代码更改为

$(data).find('string').each(function(){
    cb($(this).text());
});

但我更希望使用元素属性,例如解析文本,例如:

<photos page="1" pages="222177" perpage="1" total="222177">
    <photo id="9494235388" owner="16159287@N02" secret="f5dbd3b43a" server="7443"    farm="8" title="2013 River Cruise Day 13 Bratislava" ispublic="1" isfriend="0" isfamily="0" />
</photos>

api响应:

<methodResponse><params><param><value><string>
    <photos page="1" pages="222189" perpage="1" total="222189">
        <photo id="9494235388" owner="16159287@N02" secret="f5dbd3b43a" server="7443" farm="8" title="2013 River Cruise Day 13 Bratislava" ispublic="1" isfriend="0" isfamily="0" />
    </photos>
</string></value></param></params></methodResponse>
4

1 回答 1

1

afaik,您可以将format选项更改为"json"

var options = { 
  "api_key": API-KEY,
  "method": "flickr.photos.search",
  "accuracy": "11",
  "content_type": "1",
  "format": "json",                    //<--- JSON format
  "per_page": "1",
  "text": TITLE
}

我认为这样解析响应会更容易...

$.get(url, function(data) {
    $.each(data.photos, function(){
        console.log(this);  //<-- your photo object
    });
});
于 2013-08-12T12:09:00.320 回答