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

Source: Internet
Author: User
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) ;//objectconsole.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?) )。


Reply to discussion (solution)

var str2 = eval (' (' +str+ ') ');

var str2 = eval (' (' +str+ ') ');
This is where I turn the string into an object, what's wrong?

jquery converts JSON objects into URL parameter strings in AJAX operations
So you can accept it in PHP in a normal way.

JQ provides serialization functions Htmlserialize, Serialize

jquery converts JSON objects into URL parameter strings in AJAX operations
So you can accept it in PHP in a normal way.

JQ provides serialization functions htmlserialize, Serialize I mean, how do you directly turn the JS object into an array on this side of PHP? Normal use, is normal use, the data type is swollen conversion?

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 ('  data ' = =   Array (    ' a ' = ' = ' B ',    ' aa ' = ' BB '  ),)
Assigning a value to an $_post array

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.

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. That's good, it's got a lot of harvest. Especially your "engage" word used very in place!

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 ('  data ' = =   Array (    ' a ' = ' = ' B ',    ' aa ' = ' BB '  ),)
Assigning a value to an array of $_post thank you, that's it!

That's good, it's got a lot of harvest. Especially your "engage" word used very in place!
Oh. A universal verb. Think of oneself as carrying cement to build a house, that "engage" the word blurted out ...


That's good, it's got a lot of harvest. Especially your "engage" word used very in place!
Oh. A universal verb. Think of oneself as carrying cement to build a house, that "engage" the word blurted out ... Work!

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. I don't know if you can see this in the handbook today, it's easier to understand:
http://www.php.net/manual/zh/reserved.variables.httprawpostdata.php

The value that the POST passes should be: $HTTP _raw_post_data
And the $_post we're using is formatted data on this basis.


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. I don't know if you can see this in the handbook today, it's easier to understand:
http://www.php.net/manual/zh/reserved.variables.httprawpostdata.php

The value that the POST passes should be: $HTTP _raw_post_data
And the $_post we're using is formatted data on this basis.

Thank you, I also study.

  • 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.