[英]Ajax not making function call to php function
我在腳本中具有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);
}
}
現在,使用此代碼,我獲得了成功的成功。 但不下載文件
您得到的是404,因此控制器可能存在路由問題,或者控制器操作中某處出現致命錯誤。 調試控制器動作的一種方法是嘗試將error_log放在exportToCSVAction函數的開頭,如下所示:
error_log("BEGINNING OF FUNCTION");
然后檢查您的apache錯誤日志,看看它是否記錄了錯誤。 如果確實記錄了錯誤,請嘗試在函數中的其他語句之后放置另一個error_log語句,看看是否已記錄這些錯誤。 如果未記錄錯誤,您將知道哪個語句導致腳本死亡。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.