title: Download file data using JQuery/AJAX
body: <p>I have a server using Java's Spring library that generates a .csv file based on the parameter <em>queryParam</em> when I do an AJAX post on the client side.</p>
  
  <pre><code>$.ajax(
  {
      url: myUrl,
      type: 'POST',
      contentType: 'application/json',
      data: queryParam,
      success: function(response)
      {
          console.log(response);
      },
      error: function(jqXHR, textStatus, errorThrown)
      {
          console.error("error in query()");
          console.log(jqXHR);
          console.log(textStatus);
          console.log(errorThrown);
      }
  });
  </code></pre>
  
  <p>The server-side code attaches the appropriate headers and writes the data <strong>directly</strong> to the response stream (please note I have to do it that way, since I cannot make a file on the server):</p>
  
  <pre><code>@RequestMapping(value = "/query", method = RequestMethod.POST)
  public void exportSearchEvents(HttpServletResponse resp, @RequestBody QueryParams params)
  {
      Writer os=null;
  
      try
      {
          resp.setHeader("Content-Disposition", "attachment; filename=\"searchresults.csv\"");
          resp.addHeader("Content-Type", "application/force-download");
          resp.addHeader("Content-Type", "application/csv");
          resp.addHeader("Content-Type", "application/x-download");
  
          resp.setContentLength(580);
          resp.addHeader("Pragma", "public");
          resp.addHeader("Expires", "0");
          resp.addHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
          os = resp.getWriter();
          createFile(os, params); //writes the .csv data to the response PrintWriter object
          os.flush();
          os.close();
      }
      catch(Exception e)
      {
          logger.info("Error creating .csv file. " + e.getMessage());
          logger.error("Error creating .csv file.", e);
      }
  
      logger.info("got to here. " + resp.toString());
  
      if(os==null)
      {
          logger.info("Error creating .csv file: null");
          logger.error("Error creating .csv file: null");
          return;
      }
      return;
  }
  </code></pre>
  
  <p>When it returns, I get a string back of the results (that should be in the .csv), but the download option box doesn't pop up.</p>
  
  <p>Also, I should mention, I <strong>cannot</strong> create the file on the server and link to it (i.e. <em>document.location.href = /server/createdfile.csv</em> or whatever) from the browser due to file permissions that cannot be changed.  And I have also tried various forms of hidden iframes that href to various places, but since I have to somehow give <em>queryParam</em> to the server, I can't do that (correct me if I'm wrong).<br>
  And because of limitations with Spring, simple POSTs and GETs won't work (I'm assuming it's an issue with Spring):</p>
  
  <pre><code>$.get(myUrl, queryParam, function(response)
  {
      console.log(response);
  });
  </code></pre>
  
  <p>and</p>
  
  <pre><code>$.post(myUrl, queryParam, function(response)
  {
      console.log(response);
  });
  </code></pre>
  
  <p>give me:</p>
  
  <pre><code>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().
  </code></pre>
  
  <p>Also note that I'm avoiding Flash, so please, no answers regarding Flash.</p>
  
  <p>Thanks in advance.</p>