【问题标题】:Why is my Ajax not sending the information to the external file?为什么我的 Ajax 没有将信息发送到外部文件?
【发布时间】: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


【解决方案1】:

我不认为,您的 ajax 调用存在问题。您的$.post 似乎很好。但问题可能是因为您的成功回调处理程序。

在您的 ajax 请求成功完成后,您将 pdfajax.php 设置为 iframe 的源,这没有任何意义。因为,这将触发对 URL 'pdfajax.php' 的新 GET 请求,并且它根本不包含任何请求参数。

理想情况下,您的数据对象将包含从“pdfajax.php”收到的响应,您可以使用它在 div 左右显示。

function( data ){
    $('#report_result').html(data); // this can be used for html response not for pdf.
}

但是,如果您想要 iframe 中的数据,我认为不需要 Ajax。您可以直接为 iframe 设置 src,如下所示。

    $(".pdf-report").dialog({
    autoOpen: false,
    modal: true,
    draggable: true,
    resizable: true,
    width: 700,
    height: 650,
    dialogClass: "no-close",
    buttons: {
        "PDF": function(){
            var send_html = $('#report_result').html();
            $('#ireport_result').prop('src', 'pdfajax.php?code='+send_html);
        },
        "Close": function() {
            $(this).dialog("close");
        }
    }
});

而且您的 php 文件几乎没有问题。请求参数是code 不是send_html 并且您的请求类型是GET 不是POST

require_once 'core/init.php';
require_once 'tcpdf/tcpdf.php';
$html = input::get('code');

$method = $_SERVER['REQUEST_METHOD'];
if(strtolower($method) == 'get'){
    $pdf = new TCPDF();
    $pdf->AddPage();
    $pdf->writeHTML($html, true, false, false, false, 'C');
    $pdf->Output('example_001.pdf', 'I');
}

【讨论】:

  • 这通过使程序成功执行功能而起作用,但它们没有成功返回 pdf。我更改了选择器以同意 html。 dropbox.com/s/w252oijxzz0ub8o/… 这是第一个代码的结果。下图是第二个代码的结果:dropbox.com/s/w27sfe3v019jua6/…
  • 我编辑了帖子以包含 html。你说它是一个 iframe 是对的。我稍微调整了代码,使 div 的 ID 为“report_result”,而 iframe ID 为“ireport_result”
  • 您不能在 div 中设置 pdf。由于您试图将 pdf 内容直接输出到 div 中,因此您会在第一张图像中得到那些乱码。尝试使用 #ireport_result 而不是 #report_result 的第二个代码
  • 我正在尝试但它不起作用,它显示了这个结果:dropbox.com/s/w27sfe3v019jua6/…。我将更新原始帖子以显示我现在使用的代码。
  • 顺便感谢您的帮助,这对我来说似乎是正确的方向
猜你喜欢
  • 1970-01-01
  • 2012-07-16
  • 2013-09-27
  • 1970-01-01
  • 2011-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-12
相关资源
最近更新 更多