2017-09-06 68 views
0

我在引导模式中有一个打印按钮。如何在ajax调用后返回的html页面调用window.print

$('#printST').click(function() { 
    $.ajax({ 
     type: 'GET', 
     url: 'print', 
     data: formData, 
     dataType: 'html', 
     success: function (html) { 
      // how to print the content of html? 
     }, 
     error: function (data) { 
      console.log('Error:', data); 
     } 
    }); 
}); 

我想打印返回的数据。

在我的打印页

<script type="text/javascript"> 
window.onload = function() { window.print(); } 

在我的控制器

... 
    return View::make('pages.print'); 

我怎么能打印AJAX调用后该网页的内容?

+0

那么你需要将它添加到页面来打印它。 – epascarello

+0

@epascarello如何返回ajax调用中的html页面? print()函数已经存在。 – Crazy

+0

为什么你不只是在新窗口中打开它?用Ajax加载页面什么也不做,它是纯文本。 – epascarello

回答

0

呼叫和返回return View::make('pages.print');不会帮助,因为它只会创建View类的实例。你需要HTML字符串,并为您需要调用render()

这样对你控制器

$view = View::make('pages.print'); 
return $view->render(); 

它将返回HTML字符串的Ajax功能

在你的Ajax调用

$('#printST').click(function() { 
    $.ajax({ 
     type: 'GET', 
     url: 'print', 
     data: formData, 
     dataType: 'html', 
     success: function (html) { 
      w = window.open(window.location.href,"_blank"); 
      w.document.open(); 
      w.document.write(html); 
      w.document.close(); 
      w.window.print(); 
     }, 
     error: function (data) { 
      console.log('Error:', data); 
     } 
    }); 
}); 

它会打开print window返回HTML从控制器

希望这会有所帮助!

+0

Bhai Bhai .. Jor !!!(#) –