问题
我正在使用jQuery将(相对较大的)数据发送到一个Web系统,我正在从Ubuntu迁移到CentOS(这是一个痛苦的过程)。问题在于接收到的数据被截断了。从服务器发送相同的数据到客户端不会出现截断。
被“发送”的数据量(即调试JavaScript时看到的数据量)为116,902字节(正确的数据量),而接收到的数据量大约为115,668字节:这个数字似乎有所变化,这让我相信问题可能与时间有关。事务完成(接收、响应)大约需要3.1秒,这并不是很长的时间。是否有任何设置我应该检查?
除此想法外,我的PHP安装已配置为接受8M的post数据并使用128M的物理内存,这似乎足够了。
以下是jQuery代码。我非常确定这不是问题,但我已按要求包含了它。
接收:
function synchronise_down()
{
$.ajax({url: "scripts/get_data.php",
context: document.body,
dataType: "json",
type: "POST",
success: function(result)
{
// Fix the state up.
update_data(result);
// Execute on syncronise.
execute_on_synchronise();
},
error: function(what, huh)
{
IS_WAITING = false;
}
});
}
发送中:
function synchronise_up()
{
var serialised = MIRM_MODEL.serialise();
LAST_SERIALISED = new Date().getTime();
$.ajax({url: "scripts/save_model.php",
context: document.body,
dataType: "json",
data: {"model":serialised},
type: "POST",
success: function(result)
{
// Fix the state up.
update_data(result, true);
// Execute on syncronise.
execute_on_synchronise();
},
error: function(what, huh)
{
IS_WAITING = false;
}
});
}
解决方法(不算是完美解决方案)
编辑:我已经“修复”了这个问题,但并没有找到问题所在以及如何解决它。这是一个有趣的问题,所以我将描述一下我的解决方法并保留此问题。
我的做法是,不让jQuery直接处理大数据的序列化,而是自己先处理好数据,实质上进行了两次序列化。具体代码如下:
function synchronise_up()
{
var serialised = JSON.stringify(MIRM_MODEL.serialise());
LAST_SERIALISED = new Date().getTime();
$.ajax({url: "scripts/save_model.php",
context: document.body,
dataType: "json",
data: {"model":serialised},
type: "POST",
success: function(result)
{
// Fix the state up.
update_data(result, true);
// Execute on syncronise.
execute_on_synchronise();
},
error: function(what, huh)
{
IS_WAITING = false;
}
});
}
重要的一行是:
var serialised = JSON.stringify(MIRM_MODEL.serialise());
现在,当它到达服务器时,我需要解码这些数据,因为它已经被序列化两次。这种“解决方案”会增加一些成本:发送更多的数据,执行更多的工作。问题仍然存在:什么是问题,以及真正的解决方案是什么?