Re: [問題] 如何使用 jQuery.parseJSON
※ 引述《gaekeamql (芋頭)》之銘言:
: JSON 不能跨SERVER 只能用JSONP
: $.getJSON("http://xxxxx/index.php?callback=?",
: function(data){
: });
這句話不太對 應該是說jquery getJSON只能用JSONP跨網域
JSON是可以用來跨網域的
常見的方法是事先載入或動態載入一段<script src="" rel="nofollow">http://"></script>
(jQuery可用getScript跨網域載入js)
這段script隨便server端愛怎麼寫就怎麼寫
而client端只要知道回傳值 就可以很方便的拿來應用
以歌詞API為例 http://lyrics.wikia.com/LyricWiki:REST
可寫成json
song = {
'artist':'Slipknot',
'song':'Before I Forget',
'lyrics':'Go!\n\nStapled shut - Inside an outside world and I\'m\nSealed',
'url':'http://lyrics.wikia.com/Slipknot:Before_I_Forget'
}
或js function
function lyricwikiSong(){
this.artist='Slipknot';
this.song='Before I Forget';
this.lyrics='Go!\n\nStapled shut - Inside an outside world and I\'m\nSeale';
this.url='http://lyrics.wikia.com/Slipknot:Before_I_Forget';
}
var song = new lyricwikiSong();
以上兩個跨網域的方法 雖然不是常見的jsonp形式 但一樣有著jsonp的精神
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.122.30.198
推
12/25 01:37, , 1F
12/25 01:37, 1F
討論串 (同標題文章)
完整討論串 (本文為第 3 之 3 篇):