Home > Backend Development > PHP Tutorial > PHP's json format and js cross-domain calling code

PHP's json format and js cross-domain calling code

WBOY
Release: 2016-07-25 09:03:38
Original
1039 people have browsed it
  1. function jsontest()

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

  5. var json2 = [['crystal','20'],['candy','24 ']];

  6. alert(json2[0][0]);
  7. }

Copy code

This function, the first alert(json[1].username); will prompt "candy". The json variable is an array object. So it needs to be called in the format of obj.username. 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:

  1. $arr = array (

  2. array (
  3. 'catid' => '4',
  4. 'catname' => 'Rongrong',
  5. 'meta_title' => ' Rongrong Blog'
  6. ),

  7. array (

  8. 'catid' => '6',
  9. 'catname' => 'climber',
  10. 'meta_title' => 'Climbing or',
  11. )
  12. );
  13. $jsonstr = json_encode($arr);
  14. echo $jsonstr;

Copy code

In this code, $arr is an array, we use json_encode converts $arr to json format. This code will output: [{"catid":"4","catname":"u7a0bu7a0b","meta_title":"u7a0bu7a0bu535au5ba2"},{"catid":"6","catname":"climber","meta_title":"u6500u767bu8005 "}] 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. The above array will be printed out again.

  1. $jsonstr = json_encode($arr);
  2. $jsonstr = json_decode($jsonstr);
  3. print_r($jsonstr);
Copy code

Next, take a look at php json data and js How json data calls each other.

Create a new file php_json.php:

  1. $arr = array (

  2. array (
  3. 'catid' => '4',
  4. 'catname' => 'Rongrong',
  5. 'meta_title ' => 'Rongrong Blog'
  6. ),

  7. array (

  8. 'catid' => '6',
  9. 'catname' => 'climber',
  10. 'meta_title' => 'Climber',
  11. )
  12. );
  13. $jsonstr = json_encode($arr);
  14. -----The following is written outside the php range-----
  15. var jsonstr=< ? = $ jsonstr ?>;

Copy the code

Note: At the end of the php_json.php file, there is the sentence var jsonstr=;. This is to assign json format data to the jsonstr variable. Create another file json.html:

Copy the code

In this way, when viewing json.html, loadjson(jsonstr) will prompt "Rongrong ” and “climber”. This also implements js cross-domain calling.



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template