我在脚本中有ajax函数:
$.ajax({
url: 'http://www.somesitename.com/admin/exportToCSVAction',
type: 'GET',
data:{},
cache: false,
success: function() {
alert("sucess");
},
error: function () {
alert("error");
}
});
php中的exportToCSVAction函数:
public function exportToCSVAction()
{
$exportBatch = 10;
$order = $this->getTableGateway('order');
$select = new Select();
$select->from('order');
$data = $order->selectWith($select)->toArray();
$batchDir = __DIR__ . '/../../../../../data/export/batch/' . $exportBatch;
mkdir($batchDir);
$fileNameWithFilePath=$batchDir . '/order2.csv';
if (file_exists($fileNameWithFilePath))
{
$this->downloadOrderCSVAction($fileNameWithFilePath);
}
else
{
$csvFile = fopen($batchDir . '/order2.csv', 'w');
$i = 0;
foreach($data as $record) {
if($i==0) fputcsv($csvFile, $this->getCsvHeader($record));
fputcsv($csvFile, $this->updateCsvLine($record));
$i++;
}
fclose($csvFile);
}
}
但每当它将我的错误视为警告时。
当我通过链接直接运行时:
http://www.somesite.com/admin/exportToCSVAction
它正确地返回我的结果。 (按预期下载文件)
但是通过ajax,它会将Error作为警告。
通过检查元素网络选项卡,我得到以下信息:
请帮帮我。
我在哪里弄错了?
注意:
我也试过从ajax中删除数据,但没有效果
修改:
$.ajax({
url: 'http://www.somesitename.com/admin/exportToCSV',
type: 'GET',
data:{},
cache: false,
success: function() {
alert("sucess");
},
error: function () {
alert("error");
}
});
exportToCSVAction function in php as:
public function exportToCSVAction()
{
$exportBatch = 10;
$order = $this->getTableGateway('order');
$select = new Select();
$select->from('order');
$data = $order->selectWith($select)->toArray();
$batchDir = __DIR__ . '/../../../../../data/export/batch/' . $exportBatch;
mkdir($batchDir);
$fileNameWithFilePath=$batchDir . '/order2.csv';
if (file_exists($fileNameWithFilePath))
{
//Code to download file
}
else
{
$csvFile = fopen($batchDir . '/order2.csv', 'w');
$i = 0;
foreach($data as $record) {
if($i==0) fputcsv($csvFile, $this->getCsvHeader($record));
fputcsv($csvFile, $this->updateCsvLine($record));
$i++;
}
fclose($csvFile);
}
}
现在有了这个代码,我得到了alet成功。但不下载文件
答案 0 :(得分:0)
你得到的是404,所以要么你的控制器存在路由问题,要么你的控制器动作中出现致命错误。调试控制器操作的一种方法是尝试在exportToCSVAction函数的开头放置一个error_log,如:
error_log("BEGINNING OF FUNCTION");
然后检查您的apache错误日志,看看它是否记录了错误。如果它确实记录了错误,那么尝试在函数内部的其他语句之后添加另一个error_log语句,并查看是否记录了这些语句。如果未记录错误,您将知道哪个语句导致脚本死亡。