title: laravel + ajax: How to show error message when excel download fails
body: <p>I use laravel excel <a href="https://docs.laravel-excel.com/3.1/getting-started/" rel="nofollow noreferrer">https://docs.laravel-excel.com/3.1/getting-started/</a><br />
  Now I can download excel files. But how to do if download fails?</p>
  <p><strong>OrdersController</strong></p>
  <pre><code>public function export()
  {
      $data = $this-&gt;request-&gt;all();
      
      return $this-&gt;orderService-&gt;exportOrders($data);
  }
  </code></pre>
  <p><strong>OrderService</strong></p>
  <pre><code>public function exportOrders($data)
  {
      ...
      return (new OrderExport($data))-&gt;download($filename, \Maatwebsite\Excel\Excel::XLSX);
  }
  </code></pre>
  <p><strong>OrderExport</strong><br />
  not important here...</p>
  <p><strong>orders.blade.php</strong></p>
  <pre><code>//Export: Download
  $('#button-export-save').on('click', function () {
      var dataString = $('form').serialize();
      var ext = $('#input-excel-format').val();
  
      $.ajax
      ({
          type: &quot;POST&quot;,
          url: &quot;{{ route('lang.admin.sales.orders.export') }}&quot;,
          data: dataString,
          cache: false,
          xhrFields:{
              responseType: 'blob'
          },
          beforeSend: function () {
               $('#loading').css(&quot;display&quot;, &quot;&quot;);
               $('#button-export-save').attr(&quot;disabled&quot;, true);
          },
          success: function(data)
          {
              var link = document.createElement('a');
              link.href = window.URL.createObjectURL(data);
              link.download = 'cto.' + ext;
              link.click();
              $('#modal-option').modal('hide');
          },
          complete: function () {
              $('#loading').css(&quot;display&quot;, &quot;none&quot;);
               $('#button-export-save').attr(&quot;disabled&quot;, false);
          },
          fail: function(data) {
              alert('Not downloaded');
          }
      });
  });
  </code></pre>
  <p>If download failes, I want to return some error message.</p>
  <p><strong>OrderService</strong></p>
  <pre><code>public function exportOrders($data)
  {
      ...
  
      if(some condition){
          return 'No data'; 
      }else{
          return (new OrderExport($data))-&gt;download($filename, \Maatwebsite\Excel\Excel::XLSX);
      }        
  }
  </code></pre>
  <p>How to alert the error message 'No data'?</p>
  <p>The 'success' part of ajax, executes everytime, even there is no data exist.<br />
  The 'fail' part of ajax, never executes.</p>