如果我使用 Jquery Ajax 函数来解析 last.fm API 一切正常。
jQuery函数:
$.ajax({
url: "http://ws.audioscrobbler.com/2.0/?method=album.search&album=believe&limit=2&api_key=b25b959554ed76058ac220b7b2e0a026&format=json",
cache: true,
dataType: "json",
success: function(data_album) {
$.each(data_album.results.albummatches.album, function(a_album,res_album){
$("#search-album").append("Album: "+(a_album+1)+" :"+res_album.name+"<hr />");
});
}
}
但是,如果我将 LAST.FM url 页面的 JSON 结果复制到服务器上的 php 文件中,则不起作用:
PHP 页面
<?php header('content-type: application/json; charset=utf-8');
echo '{"results":{"opensearch:Query":{"#text":"","role":"request","searchTerms":"believe","startPage":"1"},"opensearch:totalResults":"2523","opensearch:startIndex":"0","opensearch:itemsPerPage":"2","albummatches":{"album":[{"name":"Believe","artist":"Disturbed","id":"1418894","url":"http:\/\/www.last.fm\/music\/Disturbed\/Believe","image":[{"#text":"http:\/\/userserve-ak.last.fm\/serve\/34s\/53553281.png","size":"small"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/64s\/53553281.png","size":"medium"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/126\/53553281.png","size":"large"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/300x300\/53553281.png","size":"extralarge"}],"streamable":"1","mbid":"ed32813a-cd20-4bb3-b3fd-bca6161deb17"},{"name":"Make Believe","artist":"Weezer","id":"2025180","url":"http:\/\/www.last.fm\/music\/Weezer\/Make+Believe","image":[{"#text":"http:\/\/userserve-ak.last.fm\/serve\/34s\/53692665.png","size":"small"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/64s\/53692665.png","size":"medium"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/126\/53692665.png","size":"large"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/300x300\/53692665.png","size":"extralarge"}],"streamable":"1","mbid":"f4fe4c6a-d5b8-45a5-b24f-2c3b9dda8811"}]},"@attr":{"for":"believe"}}}';
?>
jQuery函数:
$.ajax({
url: "mypage.php",
cache: true,
dataType: "json",
success: function(data_album) {
$.each(data_album.results.albummatches.album, function(a_album,res_album){
$("#search-album").append("Album: "+(a_album+1)+" :"+res_album.name+"<hr />");
});
}
}
为什么?