Background profile. php code:
Copy codeThe Code is as follows:
<? Php
$ Arr = array (
'Firstname' => iconv ('gb2312', 'utf-8', 'sincerity '),
'Lastname' => iconv ('gb2312', 'utf-8', 'Do not disturb '),
'Contact '=> array (
'Email '=> 'fcwr @ jb51.net ',
'Website' => 'HTTP: // www.jb51.net ',
)
);
// JSON
$ Json_string = json_encode ($ arr );
// Note that double quotation marks can be used to process the variables in the variable.
Echo "getProfile ($ json_string )";
?>
It should be noted that, in non-UTF-8 encoding, Chinese characters will not be encode, the results will be null, So if you use gb2312 to write PHP code, then we need to use iconv or mb to convert the content containing Chinese characters into UTF-8 and then perform json_encode.
Front index.html code:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function getProfile (str ){
Var arr = str;
Document. getElementById ("firstname"). innerHTML = arr. firstname;
}
</Script>
<Body>
<Div id = "firstname"> </div>
</Body>
<! -- Use JSON to implement cross-Origin data calling. For example, you can change "profile. php" to "http: // another domain name/profile. php" to see cross-Origin data calling. -->
<Script type = "text/javascript" src = "profile. php"> </script>
The JSON-format data is directly assigned to the variables in javascript and becomes an array. The next operation will be very convenient. If XML is used for data transmission, subsequent operations will be inconvenient.
Obviously, when index.html calls profile. php, The JSON string is generated and passed into getProfile as a parameter, and then the nickname is inserted into the div. This completes a cross-Origin data interaction.
Call index.html
Output: FEICHENG