Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

An example of php calling json across domains


May 08, 2021 JSON



JSON is not much different from XML, but JSON has a wider range of applications, which is cross-domain data calls. B ecause of security issues, AJAX does not support cross-domain calls, which can be cumbersome to call data under different domain names. The following example is enough to show how php is called across domains with json.

index.html

The code is as follows:
<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"></script>
The tuned file .php

The code is as follows:
<?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 profele.php, the JSON string is generated and passed in as an argument to getProfile, and then the nickname is inserted into the div, so that a cross-domain data interaction is done, isn't it particularly simple?


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

Let's start with a js function

The code is as follows:
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]); 
} 
This function, the first alt (json.username); " Candy" is prompted. T he json variable is an array object. S o call it in a format like obj.username.
The second alert (json2 . A "crystal" is prompted. T he json2 variable is a complete json format. B oth the json and json2 variables have the same effect, but jason2 is significantly thinner than jason.
This is the johnson format of JavaScript.
Let's take a look at the json format in php.
Or look at a piece of code first
The code is as follows:
$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 convert $arr to json json_encode using a json_encode.
This code outputs:
[{"catid":"4","catname":"\u7a0b\u7a0b","meta_title":"\u7a0b\u7a0b\u535a\u5ba2"},{"catid":"6","catname":"climber","meta_title":"\u6500\u767b\u8005"}]
This is php's handling of json data.
For jason data, php can also convert json_decode jason data into an array using the "s" () function.
For example, in the above code, we json_decode with a function. T he array above is printed again.
$jsonstr = json_encode($arr);
$jsonstr = json_decode($jsonstr);
print_r($jsonstr);
Next, let's look at how php json data and js json data call each other.

Let's create a php_json.php file
The code is as follows:
$arr = array ( 
array ( 
'catid' => '4', 
'catname' => '程程', 
'meta_title' => '程程博客' 
), 

array ( 
'catid' => '6', 
'catname' => 'climber', 
'meta_title' => '攀登者', 
) 
); 
$jsonstr = json_encode($arr); 
----- is written below outside the php interval-----
var jsonstr=< ? = $jsonstr ? & gt;;

PS: at php_json.php end of the file var jsonstr? = $jsonstr ? & gt;; T his sentence. T his is the data in the json format assigned to the jsonstr variable.

Let's build another json .html file

The code is as follows:
<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> 
So when we look .html json's data, loadjson (jsonstr) prompts "process" and "climber"
This also implements js cross-domain calls.