How does PHP receive the ajaxPOSTjson object to PHP?

Source: Internet
Author: User
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:





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.