Home >Backend Development >PHP Tutorial >PHP uses json data to implement cross-domain operation examples_PHP tutorial
This article briefly introduces examples of how to use php to use json data to achieve cross-domain operations. Students in need can refer to it.
: A certain website uses a php script in the backend to obtain data in JSON format, hands it to the frontend javascript for processing, and uses JSON to implement cross-domain calls of the data
Backend profile.php code:
代码如下 | 复制代码 |
$arr = array( 'firstname' => iconv('gb2312', 'utf-8', '非诚'), 'lastname' => iconv('gb2312', 'utf-8', '勿扰'), 'contact' => array( 'email' =>'fcwr@hzhuti.com', 'website' =>'http://www.hzhuti.com', ) ); //将一个数组JSON $json_string = json_encode($arr); //此处注意,双引号能对里面的变量当变量进行处理,单引号则不会 echo "getProfile($json_string)"; ?> |
It should be pointed out 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 Convert mb to UTF-8 and then json_encode.
Front index.html code:
The code is as follows
|
Copy code
|
||||
|
Call index.html
http: //www.bkjia.com/PHPjc/444711.html