[PHP] several cases of JSON value transfer and PHP receiving, and several situations of json
The background network of Chrome is used to analyze$. Ajax() Several situations when the json data is post to PHP:
NoGet json data through $ _ POST and $ _ REQUEST in PHP, that is
$ Json = $ _ POST ['json']; // empty ($ json) is 1
Note: by default, PHP only recognizes the standard data types of application/x-www.form-urlencoded. Therefore, content such as text/xml or soap or application/octet-stream cannot be parsed, if you use the $ _ POST array to receive the message, it will fail.
Case:
JsNoAdd the attribute contentType: "application/json; charset = UTF-8 ",
Var submit_sync = function () {$. ajax ({type: "post", url: 'add-post-json.php ', async: false, // use the synchronous method // 1 needs to use JSON. stringify otherwise the format is a = 2 & B = 3 & now = 14... // 2 requires forced type conversion. Otherwise, the format is {"a": "2", "B": "3"} data: JSON. stringify ({a: parseInt ($ ('input [name = "a"] '). val (), B: parseInt ($ ('input [name = "B"] '). val (), now: new Date (). getTime () // do not add a comma to this line}), dataType: "json", success: function (data) {$ ('# result '). text (data. result);} // do not add a comma in this line });}
- After $ GLOBALS ['HTTP _ RAW_POST_DATA '] is usedNoObtain the data, that is
$ Json = $ GLOBALS ['HTTP _ RAW_POST_DATA ']; // empty ($ json) is 1
- Use file_get_contents ("php: // input ");YesObtain data, that is
$ Json = file_get_contents ("php: // input"); // empty ($ json) is 0
Case B:
Add the attribute contentType in js: "application/json; charset = UTF-8 ",
Var submit_sync = function () {$. ajax ({type: "post", url: 'add-post-json.php ', async: false, // use the synchronous method // 1 needs to use JSON. stringify otherwise the format is a = 2 & B = 3 & now = 14... // 2 requires forced type conversion. Otherwise, the format is {"a": "2", "B": "3"} data: JSON. stringify ({a: parseInt ($ ('input [name = "a"] '). val (), B: parseInt ($ ('input [name = "B"] '). val (), now: new Date (). getTime () // do not add a comma to this line }),ContentType:"App/json; charset = UTF-8",
DataType: "json", success: function (data) {$ ('# result '). text (data. result);} // do not add a comma in this line });}
- After $ GLOBALS ['HTTP _ RAW_POST_DATA '] is usedYesObtain the data, that is
$ Json = $ GLOBALS ['HTTP _ RAW_POST_DATA ']; // empty ($ json) is 0
- Use file_get_contents ("php: // input ");YesObtain data, that is
$ Json = file_get_contents ("php: // input"); // empty ($ json) is 0
Note: php: // input allows reading original POST data. Compared with $ HTTP_RAW_POST_DATA, it puts less pressure on the memory and does not require any special php. ini settings. Php: // input cannot be used for enctype = "multipart/form-data ".
Case C:
Add the following?
header('Content-Type:application/json;charset=utf-8');
There is no impact on the results of case A and case B.
Starting from version 5.2, PHP provides native json_encode () and json_decode () functions. The former is used for encoding and the latter is used for decoding. The blog below is very detailed, mark!
Use JSON: http://www.ruanyifeng.com/blog/2011/01/json_in_php.html in PHP