title: jQuery Ajax - Displaying download dialog when content type is 'application/pdf'
body: <p>I'm using TCPDF in my CakePHP project to generate PDF files based on user input and results based on their inputs. Currently, I'm sending the user inputs to a stand-alone <code>.php</code> file via Ajax. The stand-alone <code>.php</code> file generates a PDF file based on the data sent, and then returns a PDF file, as shown in the <a href="http://www.tcpdf.org/examples.php" rel="nofollow">TCPDF examples page</a>.</p>
  
  <p>Everything works fine when I directly access the stand-alone .php file (I get a download file prompt from the browser), but when I try it via Ajax, I'm unable to replicate the same behaviour. TCPDF takes care of setting up the correct headers, so I reckon this is an issue with my jQuery. Below are my different, but unsuccessful attempts:</p>
  
  
  
  <pre><code>//attempt 1
  $.ajax({
    url: '/download.php',
    type: 'post',
    data: { type: 'generate', values: $('#table').html() },
    success: function(response) {
      console.log(response); //outputs garbled binary data on the browser console
    }
  });
  
  //attempt 2
  $.ajax({
    url: '/download.php',
    type: 'post',
    dataType: 'text',
    data: { type: 'generate', values: $('#table').html() },
    success: function(response) {
      console.log(response); //outputs empty string ""
    }
  });
  
  //attempt 3
      $.ajax({
    url: '/download.php',
    type: 'post',
    dataType: 'text',
    contentType: 'application/pdf',
    data: { type: 'generate', values: $('#table').html() },
    success: function(response) {
      console.log(response); //outputs empty string "", same as above. No download dialog.
    }
  });
  
  //attempt 4
      $.ajax({
    url: '/download.php',
    type: 'post',
    contentType: 'application/pdf',
    data: { type: 'generate', values: $('#table').html() },
    success: function(response) {
      console.log(response); //outputs empty string "", same as above. No download dialog.
    }
  });
  </code></pre>
  
  <p>How can I force the browser to show the download dialog to the user while using Ajax?</p>