The ajaxPOSTjson object is sent to PHP. how does PHP receive the value on the frontend:
$ ("# Save_config_btn "). click (function () {$. ajaxSetup ({cache: false, contentType: "application/x-www-form-urlencoded; charset = utf-8"}); var fields = $ ("# rss_form "). serializeArray (); fields = JSON. stringify (fields) // garbled characters in Chinese $. ajax ({type: "POST", url: "edit. php ", data: fields, success: function (msg) {alert (msg );}});});
PHP:
';print_r($data);?>
The JSON format obtained through json. stringify is as follows:
[{ "name":"Bill" , "value":"Gates" },{ "name":"George" , "value":"Bush" },{ "name":"Thomas" , "value":"Carter" }]
PHP. here, the POST receiving value does not know what to use. if you change the above json string to the following format, PHP can accept and directly return the array format.
{"data":[{ "name":"Bill" , "value":"Gates" },{ "name":"George" , "value":"Bush" },{ "name":"Thomas" , "value":"Carter" }]}
To sum up, I want to ask two questions:
1. how does JQuery convert the json object obtained by serializeArray () to a json string in the second format and ensure that Chinese characters are not garbled?
2. how can php directly receive json objects or json strings in the first format?
Reply to discussion (solution)
1. how does JQuery convert the json object obtained by serializeArray () to a json string in the second format and ensure that Chinese characters are not garbled?
Ensure no garbled characters. you must add them to the header.
Second array format
Var fields = $ ("# rss_form"). serializeArray ();
Var t = {};
T ['data'] = fields
Fields = JSON. stringify (t );
2. how can php directly receive json objects or json strings in the first format?
Use the first json string to submit an example: