简体   繁体   English

Ajax发布后动作未执行

[英]Action doesn't execute after ajax post

I'm trying to generate a pdf file with an image and a highcharts image in it. 我正在尝试生成其中包含图像和highcharts图像的pdf文件。 This is what I do when I click on a button: 这是我在单击按钮时所做的:

('#button').click(function() {
    var quizid = <?php echo json_encode($quizid); ?>;
    var chart = Highcharts.charts[0];

    canvg(document.getElementById('canvas'), chart.getSVG())

    var canvas = document.getElementById("canvas");
    var img = canvas.toDataURL("image/png");

    //document.write('<img src="'+img+'"/>');

    // AJAX CALL TO ACTION
    $.ajax({
        url: '/results/savepdf',
        type:"POST",
        data: {quizid: quizid, image: img},
        success: function(data) {
            console.log("ajax call succces");
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });

});

In my action I have: 在我的行动中,我有:

public function savepdfAction(){
    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();

    if(isset($_POST['quizid']))
        $quizid = $_POST['quizid'];
    if(isset($_POST['image']))
        $image = $_POST['image'];

    // SAVE THE PDF OR WORD

    // INCLUDE TCPDF LIBRARY
    require_once 'tcpdf/tcpdf.php';

    try {
        // create new PDF document
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('Nicola Asuni');
        $pdf->SetTitle('TCPDF Example 009');
        $pdf->SetSubject('TCPDF Tutorial');
        $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 009', PDF_HEADER_STRING);

        $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        $pdf->AddPage();

        $pdf->setJPEGQuality(75);

        $imgdata = base64_decode('iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABlBMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDrEX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==');

        $pdf->Image('@'.$imgdata);

        $pdf->Image('/images/logo.png', 25, 40, 154, 25, 'PNG', 'http://www.surveyanyplace.com', '', true, 150, '', false, false, 1, false, false, false);

        $horizontal_alignments = array('L', 'C', 'R');
        $vertical_alignments = array('T', 'M', 'B');

        $pdf->Output('example_009.pdf', 'I');

    }
    catch (Exception $e) {
        die ('Application error: ' . $e->getMessage());
    }
}

As you can see I just want to show a pdf file without the parameters that were sent (Just to test). 如您所见,我只想显示一个不带已发送参数的pdf文件(仅供测试)。

This doesn't work .... 这不起作用....

When I try this without the ajax call it works fine ... 当我尝试不使用ajax调用时,它工作正常...

I got this response: 我得到了这个回应:

TCPDF ERROR: Some data has already been output, can't send PDF file TCPDF错误:已经输出了一些数据,无法发送PDF文件

I searched for the error and read that you have can fix this by putting ob_end_clean(); 我搜索了错误,并读到您可以通过放入ob_end_clean();来解决此错误ob_end_clean(); before your $pdf->Output('example_009.pdf', 'I'); $pdf->Output('example_009.pdf', 'I'); . And when I did that I got another reponse that you can watch here . 当我这样做的时候,我得到了另一个答复,你可以在这里观看。

Since a .pdf file is binary data, you shouldn't use an Ajax request. 由于.pdf文件是二进制数据,因此您不应使用Ajax请求。 After a quick search I found this jQuery plugin: 快速搜索后,我找到了这个jQuery插件:

It's a very small function and it generates a temporary form with hidden fields that it submits. 这是一个非常小的函数,它会生成一个临时表单,其中包含要提交的隐藏字段。 Maybe not the most beautiful solution, but it should work. 也许不是最漂亮的解决方案,但它应该可以工作。 Haven't tested it though... 还没测试过...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM