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 msix packaging tool打包问题
    • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线