我在引导模式中有一个打印按钮。
$('#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(); }
在我的 Controller 中
...
return View::make('pages.print');
如何在调用 ajax 后打印此页面的内容?
最佳答案
调用并返回 return View::make('pages.print');
将无济于事,因为它只会创建 View
类的实例。你需要 HTML
字符串,为此你需要调用 render()
所以对于你的 Controller
$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);
}
});
});
它将打开打印窗口
,用于从 Controller 返回HTML
希望这会有所帮助!
关于javascript - 如何在ajax调用后返回的html页面上调用window.print,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46065613/