使用jquery ajax和servlet导出为ex​​cel

我在写代码导出数据(来自服务器)到excel文件时遇到问题。 我写了简单的代码来这样做。

ajax调用我的出口到excel servlet。

$.ajax({ url : "http://localhost:8800/project/abc/export", type: "POST", data : data, dataType: "text", /* contentType: "application/vnd.ms-excel",*/ success : function(data, text_status, XMLHttpRequest) { console.log("export done"); }, error : function(XMLHttpRequest, text_status, error) { console.log("error"+text_status+":::"+error); } }); 

以下是我的Servlet代码:

 @Path("/abc") public class Resource { @POST @Path("/export") @Consumes(MediaType.APPLICATION_JSON) @Produces("application/vnd.ms-excel") public Response exportResults(@Context HttpServletRequest req,@Context HttpServletResponse resp) { Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("results"); Row header = sheet.createRow(0); header.createCell(0).setCellValue("tttttt"); File f =new File("new-excel-file.xls"); FileOutputStream fos; ResponseBuilder response = null ; try { fos = new FileOutputStream(f); wb.write(fos); response= Response.ok((Object) f); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } response.header("Content-Disposition", "attachment; filename=new-excel-file.xls"); response.header("Content-Type","application/octet-stream"); return response.build(); } } 

我希望在我的ajax调用完成后要求保存文件。我可以看到ajax调用后打印的数据,但没有popup来。 有人可以帮助我这个。

我的ajax请求是POST请求,所以我不能使用window.location = url。