JS inside the object Ajax post to the PHP side directly into the array

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

  • Related Article

    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.