参考了网上的资料,写了个简单的跨域调用示例:
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)";
?>
很简单,但这个技巧很有用.