duang5049 2015-08-31 09:45
浏览 74

将大JSON数据从Javascript发送到PHP

I have a problem with which I am unsure how to deal with:

I have recovered a big JSON from an API, whose contents I need to show in my website (all ok here), but I need to return the current data shown in the website (it can be deleted, edited and modified before being sent back through my AJAX call), so that I can add that data to another website, also through an API.

I can recover and add without problems with my current logic, the problem comes when I get a big JSON from my AJAX call:

$.post('ajax/send_data.php?command=create&id=' + id, {data: superArray}, function(response){
    $('#data_table').html(response);
});

Even if I send my data like this, the max. capacity is exceeded, which results in the creation of around 30 rows of data. Changing the max_input_vars in php.ini isn't an option, I can't change those myself, plus it should be set to a really big number to fit, or have no limit...

The only solution that comes to my mind (after several others that aren't applicable to my situation) is to loop over the "array", sending the data in chunks with multiple $.post calls...

The problem is that my "array" is not an array, as php sends back an object, being it originally an associative array.

The question is... how can I loop over my object, divide into chunks, and send back to PHP through AJAX those chunks?

Here's what I was trying, using as reference Convert and Loop through JSON with PHP and JavaScript Arrays/Objects

for (var key in superArray) {
    str += JSON.stringify(superArray[key]);
    cnt++;
    if(cnt % 25 == 0){
        console.log(str);
        temparray = JSON.parse(str); // here gives an error
        $.post('ajax/send_data.php?command=create&id=' + id, {data: temparray}, function(response){
            str = "";
        });
    }
}

I can't turn back my string chunks into an object, as the key is lost in the process (Each data is created like:

undefined{ "id":"1338", "name":"24HRS ", "description":"..." })

What am I doing wrong here? is there maybe a better approach to my problem that I am maybe not aware of?

Edit:

That's what I send back from my Ajax call:

echo json_encode($html);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 lammps拉伸应力应变曲线分析
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题