An example of PHP calling json across domains
- 2020-03-29 23:45:27
- OfStack
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' => 'a@gmail.com',
'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.