json 数组未使用 Javascript post 方法发布到 PHP


json array not being posted to PHP using Javascript post method

在视图中:

function updateData(that){

    f = new test();
for (var i = 0; i < dataContext.length; i++)
{
    f.test.push(new Person(dataContext[i].test, dataContext[i].test1, dataContext[i].Line1, dataContext[i].Id));
}
        $.post("<?php echo $base_url;?>index.php/controller/function/<?php echo $details['Id'];?>", { data: JSON.stringify(f) }, function (res) {
    });
}

现在在 php 函数中,函数:

function () {
    print_r($_POST);
}

返回一个空数组。

数据基本上在javascript向导中。似乎当我单击"完成"时,页面不会自行刷新。$_POST 仅在页面首次加载时调用。

我对javascript很陌生,不确定我做错了什么,干杯。

尝试用JSON.stringify(that)代替JSON.stringify(f)

function updateData(that){
    var url="<?php echo $base_url;?>index.php/controller/function/<?php echo $details['Id'];?>";
    $.post(url, { data: JSON.stringify(that) },function (res) {
        alert(res);
    });
}

如果定义了f更新,然后像这样尝试,

function updateData(that){
    var url="<?php echo $base_url;?>index.php/controller/function/<?php echo $details['Id'];?>";
    $.post(url, { data: JSON.stringify(f) },function (res) {
        alert(res);
    });
}

给你的php function some name或调用 jquery 形式,比如,

// call updateData function in your controller
var url="<?php echo $base_url;?>index.php/controller/updateData/<?php echo $details['Id'];?>";

controller中创建一个updateData函数,例如,

function updateData($id=''){
    print_r($_POST);
}