An example of php calling json across domains

There is not much difference between JSON and XML, but JSON has a wider range of applications, that is, cross-domain data calls. Due to security issues, AJAX does not support cross-domain calls, so it is very troublesome to call data under different domain names. The following example is enough to show how php uses json to make cross-domain calls.

index.html

code show as below:

<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" rel="external nofollow" ></script>

The transferred file profile.php

code show as below:

<?php  
$arr = array(  
    'name' => 'tanteng',  
    'nick' => 'pony',  
    'contact' => array(  
        'email' => '[email protected]',  
        'website' => 'http://aa.sinaapp.com',  
    )  
);  
$json_string = json_encode($arr);  
echo "getProfile($json_string)";  
?>

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, so that a cross-domain data interaction is completed, isn't it particularly simple?

PHP json format and js json format js cross-domain call implementation code

First look at a js function 

code show as below:

function jsontest() 
{ 
var json = [{'username':'crystal','userage':'20'},{'username':'candy','userage':'24'}]; 
alert(json[1].username); 

var json2 = [['crystal','20'],['candy','24']]; 
alert(json2[0][0]); 
} 

In this function, the first alert(json[1].username); will prompt "candy". The json variable is an array object. So use the format of obj.username to call. 
The second alert(json2[0][0]); will prompt "crystal". The json2 variable is a complete json format. Both json and json2 variables achieve the same effect, but json2 is obviously much more streamlined than json. 
This is JavaScript's json format. 
Let's take a look at the json format in php. 
Let's look at a piece of code first 

code show as below:

$arr = array ( 
array ( 
'catid' => '4', 
'catname' => '程程', 
'meta_title' => '程程博客' 
), 

array ( 
'catid' => '6', 
'catname' => 'climber', 
'meta_title' => '攀登者', 
) 
); 
$jsonstr = json_encode($arr); 
echo $jsonstr; 

In this code, $arr is an array, and we use json_encode to convert $arr into json format. 
This code will output: 
[{"catid":"4","catname":"\u7a0b\u7a0b","meta_title":"\u7a0b\u7a0b\u535a\u5ba2"},{"catid":"6 ","catname":"climber","meta_title":"\u6500\u767b\u8005"}] 
This is how php handles json data. 
For json data, php can also use the json_decode() function to convert json data into an array. 
For example, in the above code, we use the json_decode function to process it. will print out the above array again. 
$jsonstr = json_encode($arr); 
$jsonstr = json_decode($jsonstr); 
print_r($jsonstr); 
Next, let's see how php json data and js json data call each other. 

We create a new php_json.php file 
code as follows: 

$arr = array ( 
array ( 
'catid' => '4', 
'catname' => '程程', 
'meta_title' => '程程博客' 
), 

array ( 
'catid' => '6', 
'catname' => 'climber', 
'meta_title' => '攀登者', 
) 
); 
$jsonstr = json_encode($arr); 

-----The following is written outside the php range----- 
var jsonstr=< ? = $jsonstr ? >; 

PS: At the end of the php_json.php file, var jsonstr=< ? = $jsonstr ? >; This sentence. This is to assign data in json format to the jsonstr variable. 
 

Let's create another json.html file 

code show as below:

<SCRIPT type=text/javascript src="php_json.php"></SCRIPT>
<SCRIPT language=javascript type=text/javascript> 
  function loadjson(_json) 
  { 
    if(_json) 
  { 
    for(var i=0;i<_json.length;i++) 
     { 
       alert(_json[i].catname); 
      } 
   } 
} 
loadjson(jsonstr) 
</SCRIPT> 

In this way, when we view json.html, loadjson(jsonstr) will prompt "Chengcheng" and "climber", 
which also realizes js cross-domain calls.

Guess you like

Origin blog.csdn.net/cdming/article/details/130144400