网上不少方案,包括:
- jsonp(最常用)
- iframe(限于同根域)
- swf辅助
- 代理
- script标签
在这里特别尝试了一下最后一种方案。
分别配置了www.a.com和www.b.com
www.a.com中index.html代码:
|
1
2
3
4
5
6
7
8
9
10
11
|
// javascript 部分<script type="text/javascript">
$(function () { var script = document.createElement("script");
script.src = "http://www.b.com/script.php";
script.onload = function () {
alert(remote.test);
}
$("head")[0].appendChild(script);
});</script>
|
www.b.com中script.php代码:
|
1
2
3
|
<?phpecho "var remote={test:'hello'};";
?> |
得到的结果是“hello”。