2017-06-05 71 views
1

我有一個碧玉報告,我需要使用AJAX但是要打印,與jQuery的AJAX調用它不工作:如何使用AJAX下載Jasper報告的PDF檔案?

$.ajax(
       { 
        method: 'POST', 
        url: 'ExportReport', 
        async : false, 
        data: { 
          typeOfClient   : _type, 
          mouthOfSelect   : _mounth 
       }, 
        success : function (html) { 
         alert("Sucess"); 
       } 

如果我使用提交表單,這項工作:

//create dynamic form 
var formularioElement = document.createElement('form'); 
formularioElement.setAttribute('id', 'formSync'); 
formularioElement.method = 'POST'; 
formularioElement.action = 'ExportReport'; 

// Create input 
var formularioInput = document.createElement('input'); 
formularioInput.type = 'text'; 
formularioInput.name = 'typeOfClient'; 
formularioInput.value = _type; 

// Create input 2 
var formularioInput2 = document.createElement('input'); 
formularioInput2.type = 'text'; 
formularioInput2.name = 'mouthOfSelect'; 
formularioInput2.value = _mouth; 

formularioElement.appendChild(formularioInput); 
formularioElement.appendChild(formularioInput2); 

// add the form to DOM 
document.body.appendChild(formularioElement); 

// submit 
formularioElement.submit(); 

這兩個代碼調用服務器端並創建PDF等於jasper,但只是form.submit()做下載文件,the ajax code不下載文件。

我可以用AJAX做到嗎?

+3

從技術上講,這個問題與使用AJAX下載PDF無異。 JasperReports與問題無關,除非您的代碼在使用JasperReports生成報告時遇到問題。 –

+1

是的,你是可靠的,我只需要下載一個帶有AJAX的pdf文件,你知道這是否可能? – sergioBertolazzo

+1

考慮使用Servlet而不是AJAX。 HTML不需要比'Download'複雜。適用於所有瀏覽器,不需要AJAX。如果要隱藏參數,請使用提交按鈕將其設置爲HTML表單,該按鈕會向Servlet發出HTTP POST請求,而不是GET請求。 –

回答

0

你不能用AJAX來完成它,因爲JS無法將檔案直接保存到計算機(出於安全原因)。