我從一箇中間人文件Flickr的API獲取數據(以避免跨域問題):字符串轉換爲JSON使用jQuery
<?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&[email protected]&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":"[email protected]", "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"})
我能怎麼做將字符串轉換爲對象?
作爲@ocanal在他的回答中提到,你不需要這個代理 – chrismarx 2012-04-27 14:38:04
這也不是工作:http://pastebin.com/ZEKWTSCc我得不到輸出 – 2012-04-27 14:45:40
它的工作原理是這樣的 - http:///pastebin.com/n7WCJrGZ,看看爲什麼urs可以工作 – chrismarx 2012-04-27 15:01:30