En el archivo public/js, solicito $ajax y obtengo una cadena de respuesta, no un archivo.
Si el cliente envía una solicitud mediante ajax, el servidor responde con una cadena en lugar de un archivo xlsx.
Aquí está la parte de la solicitud:
$("#downloadXLS").on('click', function () { $.ajax({ type: 'get', url: appRoot + "transactions/downloadXLS/", success: function () { }, error: function () { } }); });Y aquí está la función de respuesta de php:
public function downloadXLS() { // Filter the excel data function filterData(&$str) { $str = preg_replace("/\t/", "\\t", $str); $str = preg_replace("/\r?\n/", "\\n", $str); if (strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; } // Excel file name for download $fileName = "transaction-data_" . date('Ym-d') . ".xls"; // Column names $fields = array('No.', 'Pay Date', 'Company'); // Display column names as first row $excelData = implode("\t", array_values($fields)) . "\n"; // Headers for download header("Content-Type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=\"$fileName\""); $allTranscations = $this->transaction->getAll(); if (is_array($allTranscations)) { // Output each row of the data foreach ($allTranscations as $key => &$trans) { $lineData = array($key + 1, $trans->pay_time, $trans->company); array_walk($lineData, 'filterData'); $excelData .= implode("\t", array_values($lineData)) . "\n"; } } else { $excelData .= 'No records found...' . "\n"; } // Render excel data echo $excelData; exit(); }No puede descargar el archivo a través de una solicitud ajax. Pruebe location.href=transactions/downloadXLS.