Front:
$ ("#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) //Chinese will be garbled $.ajax ({ type: "POST", URL: "edit.php", Data:fields, success:function (msg) { alert (msg);});} );
Php:
';p rint_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, where the Post's receive value does not know what to use, if the above JSON string is changed to the following format, then PHP can accept and directly return the array format
{"Data": [{"Name": "Bill", "Value": "Gates"},{"name": "George", "Value": "Bush"},{"name": "Thomas", "Value": "Carter"}]}
Two main questions to ask in summary:
1. How does jquery convert the JSON object obtained by Serializearray () to a JSON string in the second format and ensure that the Chinese is not garbled
2, or how PHP directly receives 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 the Chinese is not garbled
Guaranteed not garbled, need to join in the header
The second format of the array
var fields = $ ("#rss_form"). Serializearray ();
var t = {};
t[' data ' = Fields
Fields = Json.stringify (t);
2, or how PHP directly receives JSON objects or JSON strings in the first format
Submit an example with the first JSON string:
demo.php
After running:
Test.log contents are [{"Name": "Chinese", "value": "Chinese"},{"name": "Chinese", "value": "Chinese"},{"name": "Chinese", "value": "Chinese"}]
If you do not add It will be garbled.
1, Ajax itself is UTF-8 encoded transmission, so there is no need to have CHARSET=UTF-8 statement
2, the JQ post method has been sent application/x-www-form-urlencoded head, do not need you to work again
So
$.ajaxsetup ({ cache:false, contentType: "application/x-www-form-urlencoded; Charset=utf-8 " });
A paragraph is not needed. Consider the IE cache may affect the effect, you can use Cache:false as $.ajax parameters
$.ajax ({ cache:false, type: "POST",
3, Json.stringify is provided by the Json2.js class library, does not belong to the category of JQ. So JQ doesn't need to use him to process data.
Besides, Json.stringify converts an object (array) into a JSON format string, and when it is sent, the server also needs to decode
4, for the sake of explaining the problem, I made a test example
PHP End jq_server.php
<?phpecho $s = Print_r ($_post, 1); Var_dump (' Is Utf-8? ', mb_check_encoding ($s , ' Utf-8 ')); </pre>html-end <pre class= "Sycode" name= "code" ></pre> <br/> Remove fields = json.string Ify (fields); After <br/> <br/> put var fields = $ ("#rss_form"). Serializearray (); <br/> Changed to var fields = $ ("#rss_form"). Serialize (); <br/> Data: {data:fields}, <br/> to Data:fields, <br/> <br/> <br/> Since you are studying jquery Ajax use, then should do more testing </p> <p class= "Sougouanswer" > Thank you very much, thank you very much for the two-bit solution <li ><i class= "Layui-icon
">& #xe63a;