title: Post data with ajax to java servlet
body: <p>First of all, pardon me if my english is bad. I'm having some problems with sending data to my ExportServlet with ajax.</p>
  
  <p><strong>ExportServlet.java</strong></p>
  
  <pre><code>public class ExportServlet extends HttpServlet {
  private static final long serialVersionUID = 6715605810229670146L;
  
  protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
      String fileName = req.getParameter("filename");
  
      //Create ZIP file
      try {
          res.setContentType("applicatin/zip");
          res.setStatus(HttpServletResponse.SC_OK);
  
          ZipOutputStream zos = new ZipOutputStream(res.getOutputStream());
  
          //Create TXT file
          zos.putNextEntry(new ZipEntry(fileName + ".txt"));
          zos.write(getOutputData());
          zos.closeEntry();
  
          zos.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  private byte[] getOutputData() {
      byte[] result = null;
      String tmp = "Text file content";
      result = tmp.getBytes();
      return result;
  }
  }
  </code></pre>
  
  <p>The java code above works absolutely perfect. </p>
  
  <p>Then I have my ajax code that sends data to my ExportServlet (I have used filename for an example):</p>
  
  <pre><code>//Post data to ExportServlet
          $.ajax({
              type: 'post',
              url: '/export.zip',
              data: "filename = myFile",
              success:function(data){alert(data);},
              error:function(){alert('error');}
          });
  </code></pre>
  
  <p>The problem is that when the ajax function is triggered I get an error callback. I also have a link to download the ZIP file generated by ExportServlet:</p>
  
  <pre><code>&lt;a href="/export.zip"&gt;Download file&lt;/a&gt;
  </code></pre>
  
  <p>And indeed, when I click the link I get ZIP file with "null.txt" in it. How can I fix this?</p>
  
  <p>Thanks alot in advance!</p>