作者tomin (蓝蓝紫黄橘 粉灰白绿咖)
看板Ajax
标题Re: [问题] 如何使用 jQuery.parseJSON
时间Sat Dec 25 00:46:57 2010
※ 引述《gaekeamql (芋头)》之铭言:
: JSON 不能跨SERVER 只能用JSONP
: $.getJSON("http://xxxxx/index.php?callback=?",
: function(data){
: });
这句话不太对 应该是说jquery getJSON只能用JSONP跨网域
JSON是可以用来跨网域的
常见的方法是事先载入或动态载入一段<script src="
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
1F:推 gaekeamql:恩恩!! 其实都是jsonp 都是载入一个 javascript 12/25 01:37