PHP JSON format and JS cross-domain calling code

Source: Internet
Author: User
    1. function Jsontest ()

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

    5. var json2 = [' Crystal ', '],[' Candy ', ' 24 '];

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

Copy Code

This function, the first alert (json[1].username); "Candy" will be prompted. A JSON variable is an array object. So it's going to be called in a format like obj.username. A second alert (json2[0][0]); Will prompt "Crystal". The Json2 variable is completely in a JSON format. Both JSON and JSON2 variables achieve the same effect, but json2 is significantly leaner than JSON. This is the JSON format for JavaScript. Let's look at the JSON format in PHP and look at the 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 ' = ' The Climber ',
    11. )
    12. );
    13. $jsonstr = Json_encode ($arr);
    14. Echo $jsonstr;

Copy Code

In this code, $arr is an array, and we use Json_encode to convert $arr to 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 what PHP does with JSON data. For JSON data, PHP can also use the Json_decode () function to convert JSON data to an array. For example, in the above code, we use the Json_decode function to deal with. The above array is printed again.

    1. $jsonstr = Json_encode ($arr);
    2. $jsonstr = Json_decode ($JSONSTR);
    3. Print_r ($JSONSTR);
Copy Code

Next, look at how the PHP JSON data and the JS JSON data are called each other.

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 ' = ' The Climber ',
    11. )
    12. );
    13. $jsonstr = Json_encode ($arr);
    14. -----below the PHP range-----
    15. var jsonstr=<? = $jsonstr? >;

Copy Code

Note: Var jsonstr=< at the end of the php_json.php file? = $jsonstr? >; This sentence. This is to assign the data in JSON format to the JSONSTR variable. Then create a file json.html:

Copy Code

In this way, Loadjson (JSONSTR) prompts for "Rongrong" and "climber" when viewing json.html. This also implements the JS cross-domain invocation.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.