Is the object ajaxpost in js directly converted to an array in php? 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);//objectconsole.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 ?).
Reply to discussion (solution)
var str2 = eval('('+str+')');
var str2 = eval('('+str+')');
This is how I convert a string into an object. why?
During ajax operations, jquery converts a json object to the url parameter string format.
So you can accept it in a normal way in php.
Jq provides the serialization functions htmlSerialize and Serialize.
During ajax operations, jquery converts a json object to the url parameter string format.
So you can accept it in a normal way in php.
Jq provides serialization functions htmlSerialize and Serialize. I mean, how can I directly convert js objects to arrays on the php side? It is normal to use normally. is the data type swollen?
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
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.
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. I spoke very well and gained a lot. In particular, you use the word "engaging" very well!
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', ),)
Assign a value to the $ _ POST array. thank you. it turns out to be like this!
I spoke very well and gained a lot. In particular, you use the word "engaging" very well!
Haha. Omnipotent verb. Think of yourself as a cement-built house, then the word "do" blurts out...
I spoke very well and gained a lot. In particular, you use the word "engaging" very well!
Haha. Omnipotent verb. Think of yourself as a cement-built house, then the word "do" blurts out... Labor!
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. I don't know if you can see it. it's easier to understand this article in the manual today:
Http://www.php.net/manual/zh/reserved.variables.httprawpostdata.php
The value passed by post should be: $ HTTP_RAW_POST_DATA
The $ _ POST format is used to format the data.
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. I don't know if you can see it. it's easier to understand this article in the manual today:
Http://www.php.net/manual/zh/reserved.variables.httprawpostdata.php
The value passed by post should be: $ HTTP_RAW_POST_DATA
The $ _ POST format is used to format the data.
Thank you. I will also study it.