我有一个非常奇怪的行为,我不明白如何解决。
背景- (蛋糕 PHP 2.5.4)
- 用户登录网站并决定上传 zip 文件进行处理
- 用户导航到批量上传屏幕,在该屏幕中为用户提供了上传 zip 文件的预配 用户
- 上传zip文件,系统提取文件并向用户显示内容列表,并询问用户是否应继续处理
- 用户单击进程 - 在用户被重定向到另一个网页后立即触发 AJAX 调用,他们可以继续执行所需的操作
- 在服务器端,AJAX调用触发了一堆文件转换,读取等,最终导致数据存储在数据库中
- 处理 zip 文件后,将编译包含每个文件处理状态的电子邮件通知并发送给用户
问题-
- 上述一切都完美发生,但是当我尝试在间歇阶段调试各种变量时,变量显示为null或空白 - 但数据确实会保存到数据库中
- 具有正在处理的各种文件状态的最终状态数组为空,并导致致命错误
- 我尝试覆盖
AppModel::beforeSave()
方法来记录保存的数据,这也记录了空白值,但是当我检查数据库时,数据确实被保存了
几乎有一些鬼一样的行为我不理解,任何见解都是真诚的。更多注释与下面的代码内联。
法典:
AJAX 调用 -
$('#process-btn').on('click', function() {
// trigger the AJAX require to process the uploaded file on the server side
$.ajax({
url: $('#form').attr('action'),
type: 'post',
data: $('#form').serialize(),
});
window.location.replace( $(this).data('url') );
});
控制器-
myAJAXMethod() {
// 1. end the output bugger
ob_end_clean();
// 2. Send a HTTP header that closes connection
header("Connection: close");
// 3. Tell the current PHP execution script to proceed even if user aborts
ignore_user_abort(true);
// 4. Flush the entire TCP / ouput buffer
flush(); // Unless both are called !
ob_flush(); // Strange behaviour, will not work
// 5. Tell the HTTP connection that current session is closed - this doesnt actually close the user session rather closes the current request/response session
session_write_close();
$filepaths = $this->request->data['files'];
for ($i=0; $i<sizeof($filepaths); $i++) {
$filepath = $filepaths[$i];
$response = array();
$xml = $parser->parse($filepath);
$parsed = $parser->getObjectFromParsedXml($xml);
$response['parseSuccess'] = $parsed['parseSuccess'];
array_push($status, $response);
$this->log($status); // PROBLEM - this is printing out as blank
$this->log($response); // PROBLEM - this is printing out as blank
if ($response['parseSuccess']) {
// Save parsed details in database
$this->Car->create();
$this->Car->set('name', $parsed['name']);
$this->Car->set('model', $parsed['model']);
$this->Car->set('make', $parsed['make']);
$this->Car->save(); // This does save
}
}
}
应用模型.php
public function beforeSave($options = array()) {
$this->log($this->data); // PROBLEM - doesnt print anything but a save is occurring
}
更新日期:2015 年 1 月 12 日
所以我将 AJAX 调用更新为如下所示,现在它运行良好。但是,问题是AJAX函数不会将用户重定向到下一页,直到服务器端PHP脚本完成执行(这可能很长)。似乎 PHP 脚本需要保持客户端连接才能正确打印出所有日志语句。我试图弄清楚为什么 - 有什么想法吗?
AJAX 调用 -
$('#process-btn').on('click', function() {
// trigger the AJAX require to process the uploaded file on the server side
$.ajax({
url: $('#form').attr('action'),
type: 'post',
data: $('#form').serialize(),
complete: function() {
window.location.replace( $(this).data('url') );
}
});
});
我没有代表发表评论,所以我必须将其添加为答案。但是从您发布的代码中,我们看不到问题变量区域在哪里设置。变量在发送到日志记录函数之前是否有可能被覆盖?