【发布时间】:2016-05-13 20:10:20
【问题描述】:
我在验证表单后尝试返回一个 tcpdf。如果验证出现错误,php 文件将返回错误消息。如果验证成功,我想返回 PDF。麻烦的是,当返回 pdf 时,它不会显示为 PDF,只是垃圾文本。我需要对我的代码进行哪些更改?
这是我的 ajax 提交代码:
function postForm() {
var ajaxRequest;
/* Clear result div*/
$("#result").html('');
/* Get from elements values */
var values = $("#matchReportForm").serialize()
ajaxRequest= $.ajax({
url: "mReport.php",
type: "post",
data: values,
beforeSend: function() {
$('#result').html('<img src="images/indicator.gif" alt="loading..." />');
$('#btnGo').attr("disabled", "disabled");
$("#txtSecurity").focus();
}
});
ajaxRequest.done(function (response, textStatus, jqXHR){
// show successfully for submit message
$("#result").html(response);
$('#btnGo').removeAttr("disabled");
});
/* On failure of request this function will be called */
ajaxRequest.fail(function (){
// show error
$("#result").html(response);
$('#btnGo').removeAttr("disabled");
});
}
在我的 PHP 文件中,我要么回显错误消息,要么返回 pdf:
$pdf->writeHTML($report, true, false, true, false, '');
$pdf->Output('match_report.pdf', 'D');
【问题讨论】:
-
好吧,您不能简单地将 PDF 有效负载注入到 html 元素中。服务器应该如何知道这是一个 PDF 文档以及如何显示它?您将其视为字符串或 html 标记。那是行不通的。
-
这不是很有帮助,对不起,我应该提到我希望在返回 pdf 时打开它。我只希望错误消息显示在 div 而不是 pdf 中。所以我需要检查响应是 html 还是二进制,然后在 div 中显示 msg 或打开 pdf 本身。谢谢。