index.html
<script type="text/javascript">
function getProfile(str) {
var arr = str;
document.getElementById('nick').innerHTML = arr.nick;
}
</script>
<body><div id="nick"></div></body>
<script type="text/javascript" src="http://www.openphp.cn/demo/profile.php"></script>
The called file is profile.php
<?php
$arr = array(
'name' => 'tanteng',
'nick' => 'pony',
'contact' => array(
'email' => '[email protected]',
'website' => 'http://aa.sinaapp.com',
)
);
$json_string = json_encode($arr);
echo "getProfile($json_string)";
?>
When index.html calls profile.php, the JSON string is generated, passed to getProfile as an argument, and then the nickname is inserted into div.