title: How do I get browser to prompt user to save a file dynamically generated by server?
body: <p>Clients submit post requests using jquery ajax as illustrated below:</p>
  
  <pre><code>$.ajax({
      url: "/xxx?request1", 
      data: theParams,
      type: 'post',
      error: function(XMLHttpRequest, textStatus, errorThrown){
          // error handling
      },
      success: function(data){
      var theResult = JSON.parse(data);
          // success handling
      }
  });
  </code></pre>
  
  <p>Apache is configured to pass requests with <code>/xxx?</code> to a custom app. The app processes the request and returns information. Usually, this information returned as JSoN and is displayed in the "success handling" section, using jquery/javascript. The app writes the following headers (which are then returned via apache) prior to writing the JSON:</p>
  
  <pre><code>HTTP/1.0 200 OK
  Connection: close
  Server: MyServer v. 0.826
  Content-Type: text/html
  </code></pre>
  
  <p>I also have cookies working by placing the following right after the 200 OK line:</p>
  
  <pre><code>Set-Cookie: cookie_name=value; Domain=example.com; Path=/; Expires=Fri, 21-Mar-2014 01:40:22 GMT; HttpOnly
  </code></pre>
  
  <p>In one scenario, I'd like the user to be able to save the returned data (as a plain text file). But here is where I run into problems. </p>
  
  <p>If I use JavaScript to open a window and write the data to the new window, I've found that Safari and Chrome both disable the "Save as..." menu option (on Macs).</p>
  
  <p>(I have got a version working by using base64 URI, but that looks unpolished relative to the rest of the site.)</p>
  
  <p>I then began trying to include a <code>Content-Disposition: attachment; filename=file.txt
  </code> header but I could never get the browser to produce a prompt. I tried placing this header right after the 200 OK line, as well as later:</p>
  
  <pre><code>Content-Type: text/plain
  Content-Disposition: attachment; filename=file.txt
  </code></pre>
  
  <p>In addition to being not sure of the exact format of the <code>Content-Disposition</code> header, I'm also not sure if I have the <code>Content-Type</code> correct or if there's something in the ajax handler code that is preventing the save.</p>
  
  <p>I've tried to find examples where everything is put together, but can only find parts and clearly I can't get it put together correctly. Any suggestions?</p>
  
  <p>I'm targeting only modern browsers, three major platforms (Mac/Windows/Linux), and only HTML5/JavaScript on the client side.</p>