0

I am using JS to obtain a variable and then make a POST call to a Servlet. I am not using Ajax explicitly.

$('#download').click(function (e) {
....
        $.post('downloadServlet', {'var': variable});
        //e.preventDefault();
});

The Servlet has code to read a file and, in theory, send it, but I do not get any response.

   @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
....
        response.setContentType("application/octet-stream");
        response.setContentLength(...);
        response.setHeader( "Content-Disposition", String.format("attachment; filename=%s", path.getFileName()));
        try (OutputStream out = response.getOutputStream()) {
            Files.copy(path, out);
            out.flush();
        }
        System.out.println("Done");

My questions is Why is the file not sent/received? From a technical point, I am not sure I understand why it doesn't work, since I am not using Ajax. The POST method seems to execute as any normal submit form. I have read this but doesn't clarify. Any didactic explanation will be appreciated.

user1156544
  • 1,725
  • 2
  • 25
  • 51
  • $.post - sends ajax request. Also did you debug your doPost method? – eg04lt3r Oct 18 '16 at 15:09
  • :-/ that might be the reason. doPost() is called and runs successfully to the end. Is there a way to execute "non-Ajax" post from JS or how I can window.open() the results? – user1156544 Oct 18 '16 at 15:23
  • Probably this will help, I have to test it: https://stackoverflow.com/questions/2054705/non-ajax-jquery-post-request – user1156544 Oct 18 '16 at 15:26

0 Answers0