"PHP" JSON value and PHP received several cases, JSON several cases
Using Chrome's back-end network, we analyzed several cases when the JSON data was post to PHP via jquery's $.ajax():
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 synchronization // 1 need to use json.stringify otherwise format a=2&b=3&now=14 ... // 2 requires a type cast, otherwise the format is {"A": "2", "B": "3"} data:JSON.stringify ({a:parseint ($ ( ' input[name= "a"] ' ). VA L ()), B:parseint ($ ( ' input[name= "B"] ' ). Val ()), now: new Dat E (). GetTime () // Be careful not to add commas on 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
http://www.bkjia.com/PHPjc/972513.html www.bkjia.com true http://www.bkjia.com/PHPjc/972513.html techarticle "PHP" JSON transfer value and PHP received several cases, JSON in several cases using Chrome's back-end network, analysis of the $.ajax () via jquery () JSON data post to PHP when the case: no ...