繁体   English   中英

java脚本数组通过ajax(jquery)和json到php

[英]java script array of objects to php via ajax (jquery) and json

这是javascript代码:

var jsonData = JSON.stringify(testObj);
            $.ajax({
              url: '../php/functions/spesific_field_set.php',
              type: 'post',
              data: {fieldObjArray: jsonData, tableName: fieldTableName}
              }).always(SpesificPropertiesSet);

这是PHP:

$updates = mysql_real_escape_string($_POST['fieldObjArray']);
$updates = json_decode($updates, true);
$tableName = mysql_real_escape_string($_POST['tableName']);

echo $updates;

testObj是一个对象数组,我应该如何将它传递给php? 如何在php端访问这个对象数组中的数据?

谢谢!!

这是PHP文件。 这应该向您展示如何访问通过AJAX发送的$updates

$updates = $_POST['fieldObjArray'];
$updates = json_decode($updates, true);
$tableName = $_POST['tableName'];
echo $updates; // this is an array so this would output 'Array'

foreach ($updates as $key => $value) {
    echo 'Key: '.$key.' Value: '.$value.'<br />'; // to access this, just use $updates['key']
}

// example
echo $updates['something'];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM