JS inside the object Ajax post to the PHP side directly into an array?
This post was last edited by Zhoumengkang on 2013-09-12 10:03:14
Prior to the introduction of jquery
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's Code
$data = $_post[' data '];
Var_dump ($data);
The console looks at the returned results, and the output is directly an array.
Array (2) {
["A"]=>
String (1) "B"
["AA"]=>
String (2) "BB"
}
I usually need json_decode () processing on the PHP side, so that you do not need to parse the JSON format, directly get the array.
Although always know that JS object is similar to the PHP associative array, do not understand, this is what principle (JS inside the object Ajax post to the PHP directly into an array?) )。
Share to:
------Solution--------------------
is in the execution of $.post with the Serialize method will {' data ': {' A ': ' B ', ' AA ': ' BB '}}
Turned into a data[a]=b&data[aa]=bb sent to PHP
After PHP receives DATA[A]=B&DATA[AA]=BB, call the PARSE_STR function and convert it to
Array (
Array (
' A ' = ' B ',
' AA ' = ' BB ',
),
)
Assigning a value to an $_post array
------Solution--------------------
var str2 = eval (' (' +str+ ') '), which actually translates JSON directly into a JS object.
But, $.post, are you sure you can send the object to PHP? Even if you send an array up, he will also split the array into a single parameter to send up.
He first put your object into a group, and then the array into a parameter, and then in the PHP side of the parameters are merged into an array. In fact, you usually see the get method parameter string similar, have not heard that get can also get out an object.
Pure assumption, welcome to shoot bricks.