Serializearray object assignment problem for Javascript-jquery

Source: Internet
Author: User
Keywords javascript php
The current situation is that the backend uses the PHP Yii framework to return the data to the front end in view mode.

There is a form form that, by binding the OnClick event, requests the data fetched by PHP to another backend, the following sendaction.php.
Question: After assigning a value through the Serializearray () method of jquery, an object is generated, how can I assign a value to this object (such as the following code, the $results obtained by PHP, to PostData, the object is assigned a value, So that sendaction.php can accept this value)? The following code shows that the key corresponding to the results passed, but the corresponding results value is not passed over, printed on the back end, displayed as [Object 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:'results',value:results};        $.ajax({            type: "POST",            url: url,            data:postdata,            //dataType:"json",            success:function(){              alert("发送成功");            };        });    }

Reply content:

The current situation is that the backend uses the PHP Yii framework to return the data to the front end in view mode.

There is a form form that, by binding the OnClick event, requests the data fetched by PHP to another backend, the following sendaction.php.
Question: After assigning a value through the Serializearray () method of jquery, an object is generated, how can I assign a value to this object (such as the following code, the $results obtained by PHP, to PostData, the object is assigned a value, So that sendaction.php can accept this value)? The following code shows that the key corresponding to the results passed, but the corresponding results value is not passed over, printed on the back end, displayed as [Object 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:'results',value:results};        $.ajax({            type: "POST",            url: url,            data:postdata,            //dataType:"json",            success:function(){              alert("发送成功");            };        });    }

var results = 
  
   $results))?>;

Equivalent to var results = {Results:thejsonobject}; Right?
When the HTTP protocol transmits data, it is impossible to pass the array to PHP, but a specific format of the string, perhaps the JSON string, perhaps XML, perhaps the common key1=value1&key2=value2 way, no problem?
So, when the Ajax commits will inevitably convert postdata to a string, and when PostData is a nested array, there may be problems, so it leads to problems you encounter.
Suggested that the above line be modified to

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

This is equivalent to the var results = ' {results:thejsonobjct} ', is a normal string, the PHP end will be json_decode a bit later can be restored, do you think?

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