[PHP] several cases of JSON value passing and receiving by PHP, and several cases of json _ PHP Tutorial

Source: Internet
Author: User
[PHP] several cases of JSON value passing and receiving by PHP, and several cases of json. [PHP] several situations in which JSON values are transmitted and received by PHP. json uses the background network of Chrome to analyze $ through JQuery. several situations when ajax () posts json data to PHP: no [PHP] JSON value transfer and receiving by PHP, 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

Using Chrome's background network, parse analyzes several situations when json data is post to PHP through JQuery's $. ajax (): none...

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.