【发布时间】:2014-04-12 05:40:29
【问题描述】:
我的想法是我通过 ajax 将数据发送到外部 php,外部 php 将使用 tcpdf 创建一个 pdf,然后将该 pdf 返回给我。但有些不对劲。
我看不出错误在哪里。基本上,我试图通过ajax 发送两条信息。第一个是完整的 html 标记字符串,我使用 .html() 函数分配给它。第二个只是一个测试,看看它是否也会被发送。
JQuery 看起来是这样的(问题出在 PDF 按钮上):
$(".pdf-report").dialog({
autoOpen: false,
modal: true,
draggable: true,
resizable: true,
width: 700,
height: 650,
dialogClass: "no-close",
buttons: {
"PDF": function(){
/*
var send_html = $('#query_result').html();
$.post('pdfajax.php',
{
qch: 'TESTQCH',
code: send_html
},
function( data ){
$('#report_result').html(data); // assuming there is a div with id result
}
);
*/
var send_html = $('#report_result').html();
alert(send_html);
$('#ireport_result').prop('src', 'pdfajax.php?send_html='+send_html);
},
"Close": function() {
$(this).dialog("close");
}
}
});
外部文件 pdfajax.php 看起来像这样:
require_once 'core/init.php';
require_once 'tcpdf/tcpdf.php';
$html = input::get('send_html');
//echo 'test ',var_dump($qch),'<br />',var_dump($html);
$method = $_SERVER['REQUEST_METHOD'];
//echo $method;
if(strtolower($method) == 'post'){
$pdf = new TCPDF();
$pdf->AddPage();
$pdf->writeHTML($html, true, false, false, false, 'C');
$pdf->Output('example_001.pdf', 'I');
我知道什么都没有发送,因为这是结果(空白 iframe,而不是上面的文本):
哦,是的,输入类有 get 函数,它简单地说:
public static function get($item){
if(isset($_POST[$item])){
return $_POST[$item];
} elseif (isset($_GET[$item])){
return $_GET[$item];
}
return '';
}
编辑:
这是 HTML
<div class="pdf-report" title="Report Dialog">
<br />
<div id="report_result"></div>
<iframe src="tcpdf/examples/example_001.php" id="ireport_result"></iframe>
<br />
【问题讨论】:
-
使用控制台(或 FireBug 等工具)有助于识别 ajax 请求,大多数情况下它要么不会被触发,要么会得到 404 或 500 状态码。
-
它在控制台中发送它。我不知道它是否只是没有在 TCPDF 文件中呈现。
标签: javascript php jquery ajax tcpdf