title: download excel file using post request
body: <p>I've an API endpoint that takes in a JSON and returns a excel file. The endpoint directly sends the file, not the link to the file. How do I download the file using JQuery AJAX.</p>
  
  <p>Backend Code:</p>
  
  <pre><code>public function postExcel() {
  
    // Parse request body
    $reqBody = json_decode(file_get_contents('php://input'));
  
    // On Valid Request
    if(isset($reqBody, $reqBody-&gt;regno, $reqBody-&gt;offset) &amp;&amp; $reqBody-&gt;offset &lt; 100 &amp;&amp; $reqBody-&gt;offset &gt;= 0) {
      // Create and add metadata to the workbook
      $workbook = new \PHPExcel();
      $workbook-&gt;getProperties()
               -&gt;setCreator('AUScraper')
               -&gt;setTitle('AU Results')
               -&gt;setLastModifiedBy('AUScraper')
               -&gt;setDescription('generated by AUScraper')
               -&gt;setSubject('generated by AUScraper')
               -&gt;setKeywords('anna university unofficial semester result API')
               -&gt;setCategory('semester results');
  
      $worksheet = $workbook-&gt;getSheet(0);
      $worksheet-&gt;setTitle('results');
  
      // Get the results
      $results = $this-&gt;requestAU($reqBody-&gt;regno, $reqBody-&gt;offset);
  
      // Update worksheet
  
  
  
      //Output the file
      ob_clean();
      header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
      header("Content-Disposition: attachment; filename=\"results.xlsx\"");
      header("Cache-Control: max-age=0");
      header("Expires: 0");
  
      $workbookWriter = new \PHPExcel_Writer_Excel2007($workbook);
      $workbookWriter-&gt;save("php://output");
    }
  
    // On Invalid Request
    header('Content-Type: application/json');
    http_response_code(400);
    return json_encode(['error' =&gt; 'invalid request']);
  }
  </code></pre>
  
  <p>Frontend Code:</p>
  
  <pre><code>import $ from 'jquery';
  import 'materialize';
  
   $(() =&gt; {
      $('#reg_form').submit(() =&gt; {
  
    // format data to send
    let form_data = {};
    form_data.regno = $('#reg_no').val();
    form_data.offset = $('#offset').val();
  
    // Send the request
    $.ajax({
      url: 'http://localhost/git_repo/AUScraper/app/public/api/excel',
      data: JSON.stringify(form_data),
      type: 'POST',
      contentType: 'application/json',
      success: function(data) {
            var blob=new Blob([data], {type : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
            var link=document.createElement('a');
            link.href=window.URL.createObjectURL(blob);
            link.download="result_"+new Date()+".xlsx";
            link.click();
          },
      error: (xhr,status,error) =&gt; {
        let errorMessage = JSON.parse(xhr.responseText);
        Materialize.toast(errorMessage.error, 2000);
      }
    });
  
      return false;
    });
  });
  </code></pre>
  
  <p>I don't want to save the file in server beforehand, but just download it. Thanks in advance.</p>