参考了网上的资料,写了个简单的跨域调用示例:

1.HTML文件:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<script type="text/javascript">
function getProfile(str)
{
    var arr =str;//或eval(str)
    document.getElementById('TestName').innerHTML = arr.name;
}
</script>
<body><div id="TestName"></div></body>
<script type="text/javascript" src="Hello.php"></script>
</html>

注意:

<script type="text/javascript" src="Hello.php"></script>
一定要在Body之后,因为名为TestName的DIV在Body后才可以被调用.

2.PHP文件:

<?php
$arr = array(
'name' => 'garfield',
'nick' => 'Hello',
);
$json_string = json_encode($arr);
echo "getProfile($json_string)";
?> 

 

很简单,但这个技巧很有用.

posted on 2009-06-26 12:09  garfieldtom  阅读(408)  评论(0编辑  收藏  举报