我使用中间人文件从 Flickr API 获取数据(以避免跨域问题):
<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
die(json_encode( file_get_contents($_REQUEST['url']) ) );
?>
此文件由 javascript 获取:
//Flickr
var myurl = encodeURIComponent('http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=1408bff5f72a4b84b924d13e8562b6a2&photoset_id=77649470@N03&photoset_id=72157629903184261&format=json');
$.getJSON( "middle.php?url=" + myurl, function(data){
console.log(typeof data);
});
但是 console.log 显示结果是一个“字符串”而不是 JSON 对象。我尝试将其转换为 JSON 对象:
jQuery.parseJSON(data)
但控制台给了我这个错误:
Uncaught SyntaxError: Unexpected token j
这是字符串:
jsonFlickrApi({"photoset":{"id":"72157629903184261", "primary":"7115173307", "owner":"77649470@N03", "ownername":"wedocommunication", "photo":[{"id":"7115173331", "secret":"24900ff306", "server":"5447", "farm":6, "title":"Lounge", "isprimary":"0"}, {"id":"7115173307", "secret":"3435f9a983", "server":"7256", "farm":8, "title":"Hofansicht", "isprimary":"1"}, {"id":"7115173379", "secret":"7747e50597", "server":"7278", "farm":8, "title":"Konfi", "isprimary":"0"}, {"id":"6969093048", "secret":"d4389bc0e4", "server":"7055", "farm":8, "title":"Lounge", "isprimary":"0"}, {"id":"6969093086", "secret":"8e7263005b", "server":"5152", "farm":6, "title":"Eingangsbereich", "isprimary":"0"}], "page":1, "per_page":500, "perpage":500, "pages":1, "total":"5"}, "stat":"ok"})
我该怎么做才能将字符串转换为对象?