Servlet输出流不在jQuery AJAX中下载

时间:2016-08-22 19:02:06

标签: ajax servlets

我有以下代码

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

    response.setContentType("text/csv;charset=UTF-8");

    try (PrintWriter out = response.getWriter()) {

        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename=\"%s\"", "prueba.csv");
        response.setHeader(headerKey, headerValue);

        out.println("test");

        OutputStream outStream = response.getOutputStream();
        outStream.flush();
        outStream.close();
    }

}

当我从URL localhost:8080 / downloadServlet 调用servlet时,它会下载.CSV文件,但是当我从$ .ajax post请求调用servlet时它不起作用。

这是我的$ .ajax代码

$.ajax({
    url: "downloadServlet",
    type: "post",
    data: {id: 1, pass: "123"},
    error : function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
});

如何使用$ .ajax下载SERVLET OutputStream?

0 个答案:

没有答案