JSON and XML are no different, but JSON has a wider application, that is, cross-Origin data calling. Due to security issues, AJAX does not support cross-origin calls. Therefore, it is very troublesome to call data under different domain names. The following example shows how php uses json for cross-origin calls.
Index.html
Copy codeThe Code is as follows:
<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>
Profile. php
Copy codeThe Code is as follows:
<? 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, and the getProfile is passed as a parameter, and the nickname is inserted into the div. This completes a cross-Origin data interaction. Is it especially simple.