Backend profile.php code:
Copy code The code is as follows:
$arr = array(
'firstname' => iconv('gb2312', 'utf-8', ' Sincere'),
'lastname' => iconv('gb2312', 'utf-8', 'Do Not Disturb'),
'contact' => array(
'email' =>'fcwr@jb51 .net',
'website' =>'http://www.jb51.net',
)
);
//Convert an array to JSON
$json_string = json_encode($arr);
//Here Note that double quotes can treat the variables inside as variables, but single quotes will not
echo "getProfile($json_string)";
?>
It should be noted that in non-UTF-8 encoding, Chinese Characters will not be encoded, and the result will be a null value. Therefore, if you use gb2312 to write PHP code, you need to use iconv or mb to convert the Chinese content to UTF-8 and then perform json_encode.
Front-end index.html code:
Copy code The code is as follows:
Assign the JSON format data directly to the variables in javascript, and it becomes an array. It will be very convenient to operate. This If XML is used for data transmission, subsequent operations will be inconvenient.
Obviously, when index.html calls profile.php, a JSON string is generated and passed to getProfile as a parameter, and then the nickname is inserted into the div. In this way, a cross-domain data interaction is completed
Call index.html
Output :Feicheng
The above introduces the php code that uses JSON to realize cross-domain transmission of data, including the content of php code. I hope it will be helpful to friends who are interested in PHP tutorials.
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn