2016-07-15 28 views
0

我在我的網站上有一個表格,用戶需要填寫以便生成PDF文件。如果表單中存在錯誤,服務器將使用帶有錯誤列表的json對象進行響應,否則將使用PDF文件作爲響應(以字符串形式)。我試圖在Magnific-Popup(https://www.npmjs.com/package/magnific-popup)中顯示該PDF文件。在Magnific-Popup模式下顯示PDF文件

$('.preview-button').on('click', function (e) { 
     event.preventDefault(); 
     var theLink  = $(this); 
     var formElement = theLink.closest("form"); 

     $.ajax({ 
      url: theLink.attr('href'), 
      method: 'GET', 
      data: formElement.serialize(), 
      success: function (response, textStatus, jqXHR) { 
       var contentType = jqXHR.getResponseHeader("content-type") || ""; 
       if(contentType.indexOf('pdf') > -1){ 
        // How can I tell to magnific popup to display the response as PDF? 
        $.magnificPopup.open({ 
         // tell magnific popup to use the response... 
         type: 'iframe', 
         closeOnBgClick: false 
        }); 
       } 
       else{ 
        if (response.hasOwnProperty('errors')){ 
         // Form has error, show them 
         alert(response.errors); 

        } 
       } 
      } 
     }); 

    }); 

回答

0

這可能是有人用同樣的問題很有用:而不是發送PDF文件作爲響應的,我決定生成服務器的臨時pdf文件和發送的路徑文件作爲響應。然後我們可以告訴magnificPopup在iframe中顯示它。