舉例來說, 當foo.com想與bar.com進行資料交換時, 我們可以在foo的主機上加入bar上的script以取得bar的資料,
<html>
<head>
<title>Cross-Domain Communication with JSON</title>
</head>
</body>
<script type="text/javascript">
function call_bar(obj) {
 alert(obj.name);
}
</script>
<script type="text/javascript" 而在bar上的script中則提供JSON格式的資料內容, 其中call_bar為callback function的名稱, 此callback將會在foo讀取到script的內容時回呼, 並且無法控制執行順序,src="http://bar.com/?callback=call_bar"></script> <body></body> </html>
call_bar({"name":"bar"})
  - Cross-domain communications with JSONP, Part 1: Combine JSONP and jQuery to quickly build powerful mashups
- Cross-domain communications with JSONP, Part 2: Building mashups with JSONP, jQuery, and Yahoo! Query Language
- Using JSON (JavaScript Object Notation) with Yahoo! Web Services
- JavaScript Load and Execute JavaScript on Demand, by createElement
- JavaScript網路服務與純JavaScript應用之JSON資料包裹解決方案
 

沒有留言:
張貼留言