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?