我正在尝试将$ .getJSON用于函数,并且我有2个警报:inside1和inside x2。当我运行站点时,仅处理第一个警报,而代码实际上从未到达第二个警报。我该怎么做才能使代码处理该功能?我非常感谢您提供代码方面的帮助。
function get_photos(searchtag){
var apikey = '#######################';
alert('inside1');
$.getJSON('api.flickr.com/services/rest/?method=flickr.photos.search&api_key=' + apikey + '&tags=' + searchtag + '&perpage=3&format=json', function (data) {
alert('inside x2');
});
这是url返回的内容(我已经多次仔细检查过它是正确的URL):
jsonFlickrApi({
"photos": "page": 1,
"pages": 103436,
"perpage": 3,
"total": "310306",
"photo": [{
"id": "28437400284",
"owner": "20925280@N00",
"secret": "711b34701c",
"server": "8708",
"farm": 9,
"title": "20160810-DSC_0158",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
}, {
"id": "29058577365",
"owner": "20925280@N00",
"secret": "bd73425d36",
"server": "8319",
"farm": 9,
"title": "20160810-DSC_0159",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
}, {
"id": "28437396394",
"owner": "20925280@N00",
"secret": "7de3504657",
"server": "8877",
"farm": 9,
"title": "Sailing",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
}]
}, "stat": "ok"
})
正如评论中提到的@nnnnnn一样,您不能使用JSON在JavaScript中使用HTTP请求获取跨域数据。您需要使用JSONP。您需要替换$.getJSON(...)
为:
$.ajax({
url: 'http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=' + apikey + '&tags=' + searchtag + '&perpage=3&format=json',
dataType: 'jsonp',
success: function(data) {
alert('inside x2');
// You can see the response.
alert(data);
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句