Javascript 如何在ajax调用后返回的html页面上调用window.print
我在引导模式中有一个打印按钮Javascript 如何在ajax调用后返回的html页面上调用window.print,javascript,ajax,laravel,printing,Javascript,Ajax,Laravel,Printing,我在引导模式中有一个打印按钮 $('#printST').click(function () { $.ajax({ type: 'GET', url: 'print', data: formData, dataType: 'html', success: function (html) { // how to print the content of html? },
$('#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(); }
ajax调用后如何打印此页面的内容?调用并返回
返回视图::make('pages.print')代码>不会有帮助,因为它只创建视图
类的实例。您需要HTML
string,为此需要调用render()
那么你呢
...
return View::make('pages.print');
$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);
}
});
});
它将打开从控制器返回的HTML
的打印窗口
希望这会有帮助 您需要将其添加到页面中才能打印出来。@epascarello如何在ajax调用中返回html页面?print()函数已经存在。为什么不在新窗口中打开它?用Ajax加载页面没有任何作用,它是纯文本。@epascarello我不想显示要打印的页面,而要打印的页面只是一个普通的表格格式。我的意思是,为什么要在加载要打印的内容时使用Ajax。只要在一个新窗口中打开它,它就会打印出来,不需要花哨的代码。如果使用Ajax,则需要将代码附加到页面上,连接打印样式表以仅打印表,然后打印。。。。您正在加载的页面中的窗口打印代码不会起任何作用。