Home  >  Article  >  Backend Development  >  PHP uses json data to implement cross-domain operation examples_PHP tutorial

PHP uses json data to implement cross-domain operation examples_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:10:28817browse

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