Assignment of serializeArray objects in javascript-jQuery

Source: Internet
Author: User
The current situation is that the back-end uses the php yii framework to return data to the front-end in view mode. There is a form that binds the onclick event and sends the data requests obtained by php to another backend, that is, the following sendAction. php. Problem: the current situation of jQuery's se... is that the back-end uses the php yii framework to return data to the front-end through the view method.

There is a form that binds the onclick event and sends the data requests obtained by php to another backend, that is, the following sendAction. php.
Question: After the serializeArray () method of jQuery is assigned a value, an object is generated. How can I assign a value to this object? (For example, the code below, $ results obtained through php, assign values to the postdata object for sendAction. php can accept this value )? The following code shows that the results corresponding to the key is passed, but the results value is not passed. If it is printed on the backend, it is displayed as [object].

Function doAction (id) {var url = 'sendaction. php'; var postdata = $ ("# form "). serializeArray (); postdata [postdata. length] = {name: 'id', value: id}; var results =
  $ Results)?>; Postdata [postdata. length] = {name: 'result', value: results}; $. ajax ({type: "POST", url: url, data: postdata, // dataType: "json", success: function () {alert ("sent successfully ");};});}

Reply content:

The current situation is that the back-end uses the php yii framework to return data to the front-end in view mode.

There is a form that binds the onclick event and sends the data requests obtained by php to another backend, that is, the following sendAction. php.
Question: After the serializeArray () method of jQuery is assigned a value, an object is generated. How can I assign a value to this object? (For example, the code below, $ results obtained through php, assign values to the postdata object for sendAction. php can accept this value )? The following code shows that the results corresponding to the key is passed, but the results value is not passed. If it is printed on the backend, it is displayed as [object].

Function doAction (id) {var url = 'sendaction. php'; var postdata = $ ("# form "). serializeArray (); postdata [postdata. length] = {name: 'id', value: id}; var results =
  $ Results)?>; Postdata [postdata. length] = {name: 'result', value: results}; $. ajax ({type: "POST", url: url, data: postdata, // dataType: "json", success: function () {alert ("sent successfully ");};});}

var results = 
  $results))?>;

Equivalent to var results = {results: theJsonObject}; right?
When http is used to transmit data, it is impossible to pass the array to php. Instead, it is a string in a specific format, which may be a json string or xml string, maybe it is the common key1 = value1 & key2 = value2 method, OK?
Therefore, when ajax is submitted, it will inevitably convert postdata to a string. When postdata is a nested array, problems may occur, which leads to your problems.
We recommend that you change the above line

var results = '
  $results))?>';

This is equivalent to var results = '{results: theJsonObjct}'. It is a common string. After the php end is obtained, json_decode can be restored. What do you think?

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.