"PHP" JSON transfer value and PHP received several cases

Source: Internet
Author: User
Tags php language
Using Chrome's back-end network, we analyzed the $.ajax() Several cases when you post JSON data to PHP:

  • JSON data cannot be obtained in PHP via $_post and $_request, i.e.

    $json $_post [' JSON '];  // Empty ($json) is 1

Note: PHP only recognizes application/x-www.form-urlencoded standard data types by default, so content such as text/xml or soap or application/octet-stream cannot be parsed. If you use the $_post array to receive it, it will fail.

Situation A:

JS does not add attribute contenttype: "Application/json; Charset=utf-8 ",

var function () {    $.ajax ({        "post",        ' add-post-json.php ',        false//  Use synchronous mode        ///  1 need to use json.stringify otherwise format for a=2&b=3&now=14        ... // 2 Forced type conversion is required, otherwise the format is {"A": "2", "B": "3"}         data:JSON.stringify ({                              a:parseint ($ (' input[name= "a"] "). Val ()),            B:parseint ($ ( ' input[name= "B"]). Val ()            ),new//  Note Do not add commas on this line         } ),                "JSON",        function(data) {            $ (' #result '). Text ( Data.result);         // be careful not to add commas on this line     });}

    • The data cannot be obtained after using the $GLOBALS [' http_raw_post_data '], i.e.

$json $GLOBALS [' Http_raw_post_data '];  // Empty ($json) is 1

    • Use File_get_contents ("Php://input"); Data can be obtained, i.e.

$json file_get_contents ("Php://input");   // Empty ($json) is 0

Case B:

JS added attribute contenttype: "Application/json; Charset=utf-8 ",

  var  submit_sync =  function   () {$.ajax ({ Type:  "post" , url:  ' add-post-json.php ' , async:   false, //  use sync  //  1 required to use json.stringify otherwise format is a=2&b=3&now=1 4..  //  2 requires a type cast, otherwise the format is {"A": "2", "B": "3"}   Data:JSON.stringi FY ({a:parseint ($ ( ' input[name= "a"] ' ). Val ()), B:parseint ($ ( /span> ' input[name= "B"] ). Val ()), now:   new  Date (). GetTime () //  Note Do not add a comma  } to this line,  contentType:   "Application/json; Charset=utf-8 " ,   
            "JSON",            function(data) {                $ (' #result '). Text (data.result);             // be careful not to add commas on this line         });    }

    • Use $GLOBALS [' Http_raw_post_data '] to get the data, i.e.

$json $GLOBALS [' Http_raw_post_data '];  // Empty ($json) is 0

    • Use File_get_contents ("Php://input"); Data can be obtained, i.e.

$json file_get_contents ("Php://input");   // Empty ($json) is 0

Note: Php://input allows you to read the original data of the POST. Compared to $HTTP _raw_post_data, it brings less pressure to memory and does not require any special php.ini settings. Php://input cannot be used for enctype= "Multipart/form-data".

Case C:

Do you want to add the following

Header (' Content-type:application/json;charset=utf-8 ');

There is no effect on the outcome of situation A and condition B.

Starting with version 5.2, PHP native provides the Json_encode () and Json_decode () functions, which are used for encoding, which is used for decoding. The following blog is written in detail, mark!

Using json:http://www.ruanyifeng.com/blog/2011/01/json_in_php.html in the PHP language

The above describes the "PHP" JSON value and PHP received several cases, including aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    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.