The ajaxPOSTjson object is sent to PHP. how does PHP receive the value? this post is Last edited by KtosMayCry from 2014-07-0420: 30: 03 front end: & nbsp; $ (# save_config_btn ). click (function () {& nbsp; & nbs ajax POST json object to PHP. how does PHP receive values?
This post was last edited by KtosMayCry at 20:30:03, January 4 ,.
Front end:
$ ("# 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 Chinese characters
$. Ajax ({
Type: "POST ",
Url: "edit. php ",
Data: fields,
Success: function (msg ){
Alert (msg );
}
});
});
PHP:
Header ("Content-type: text/html; charset = UTF-8 ");
$ Data = $ _ POST ['data'];
Echo'';
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?
------ 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: