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; eval (the object ajax post in + str + js is directly converted into 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.