使用AJAX $ .post后下载(强制另存为对话框)

I have a webapp that simulates a terminal. Every command is posted via AJAX using the following script (portion):

AJAX/jQuery

$.post("sec/cmd_process.php", { n : n } )
    .done(function(data){
        output.append(display(data));
    });

If the user types download log into the terminal, the following script - on sec/cmd_process.php is executed:

PHP

if(isset($_POST['n'])){

    echo $_POST['n'];
    $t = explode(' ', $_POST['n']);

    if(strtolower($t[0])=='download'){
        if(!isset($t[1])) shout_error("No download specified");

        //Download Log
        elseif($t[1]=='log'){
            $stmt = $dbh->prepare("SELECT * FROM `tap_log` ORDER BY `time`");
            $stmt->execute();
            $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
            foreach($rows as $row) {
                $user = user_by_id($row['user']);
                $data.= "{$row['time']} - User {$user['id']} ({$user['name1']} {$user['name2']}) - {$row['information']} ({$row['subject']})".PHP_EOL;
            }

            $handle = fopen('log.txt','w+');
            fwrite($handle, $data);
                $path = 'log.txt';
                header('Content-Transfer-Encoding: binary');
                header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($path)).' GMT');
                header('Accept-Ranges: bytes');
                header('Content-Length:'.filesize($path));
                header('Content-Encoding: none');
                header('Content-Disposition: attachment; filename='.$path);
                readfile($path);
            fclose($handle);
        }
    }
}

What I want to happen is for the generated file, log.txt, is downloaded via the Save As... dialog. This code works if you directly visit a PHP page with those headers, but how can I make it work through jQuery/AJAX?

The simplest solution I found was to return a <script> tag forwarding the location to a forced download page:

<script type='text/javascript'>
    location.href='sec/download.php?file=log.txt&type=text';
</script>

sec/cmd_process.php

$handle = fopen('log_'.time().'.txt','w+');
fwrite($handle, $data);
    echo "Please wait while the file downloads...";
    echo "<script type='text/javascript'>location.href='sec/download.php?file=log.txt&type=text';</script>";
fclose($handle);

sec/download.php

<?php
    $filename = $_GET['file'];
    $filetype = $_GET['type'];
    header("Content-Transfer-Encoding: binary");
    header("Last-Modified: ".gmdate('D, d M Y H:i:s',filemtime($filename))." GMT");
    header("Accept-Ranges: bytes");
    header("Content-Length: ".filesize($filename));
    header("Content-Encoding: none");
    header("Content-Type: application/$filetype");
    header("Content-Disposition: attachment; filename=$filename");
    readfile($filename);