The object ajaxpost in js directly becomes an array to the php end.

Source: Internet
Author: User
Is the object ajaxpost in js directly converted to an array in php? Jqueryvar & nbsp; str & nbsp; {a: B, aa: bb}; var & nbsp; str2 & nbsp; is the object ajax post in eval (+ str +) js directly converted to an array at the php end?

This post was last edited by zhoumengkang at 10:03:14

Jquery was introduced in advance.
var str ="{'a':'b','aa':'bb'}";
var str2 = eval('('+str+')');
var type = typeof(str2);
console.log(str);
console.log(type);//object
console.log(str2);
$.post('./bb.php',{'data':str2});

Bb. php code
$data = $_POST['data'];
var_dump($data);

The returned results are displayed on the console and directly output as an array.
array(2) {
["a"]=>
string(1) "b"
["aa"]=>
string(2) "bb"
}

I usually need json_decode () processing on the php side. in this way, the array is obtained directly without parsing the json format.

Although I have always known that objects in js are similar to php's associated arrays, I still don't understand. what is the principle? (the object ajax post in js directly becomes an array to the php end ?).

Share:


------ Solution --------------------
Is executing $. post. use the Serialize method to convert {'data': {'a': 'B', 'A': 'BB '}}
Converted to data [a] = B & data [aa] = bb and sent to php
After receiving data [a] = B & data [aa] = bb, php calls the parse_str function and converts it
array (
'data' =>
array (
'a' => 'b',
'aa' => 'bb',
),
)
Assigned to $ _ POST array

------ Solution --------------------
Var str2 = eval ('+ str +'); this sentence actually directly converts json into a js object.
However, $. post are you sure you can send objects to php? Even if you send an array, it will split the array into one parameter and send it.
First, he makes your object into an array, then the array into a parameter, and then the parameters in php are merged into an array. In fact, it is similar to the get method parameter string that you usually see. you have never heard of get and get an object.

Purely hypothetical, welcome to shoot bricks.

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.