douwan2664 2015-05-04 13:59
浏览 102
已采纳

使用jQuery AJAX函数调用返回PDF的php文件

My php file: example_061.php create PDF return and everything works fine.

Now I want to call that file with jQuery AJAX to get this at my screen and I try:

$("#proba").click(function() {
  //in here we can do the ajax after validating the field isn't empty.
  $.ajax({
    url: "tcpdf/examples/example_061.php",
    type: "POST",
    async: true, 
    data: { 
      ime: $("#ime").val(),
      pozicija: $("#pozicija").val(),
      jmbg: $("#jmbg").val()
    }, //your form data to post goes here as a json object
    dataType: "html",
    success: function(response) {
      window.open('example_061.pdf');
    },  
  });        
});

Everything is fine so success function work an I get alert message but I don't get PDF file as download or at screen. How I can do that?

  • 写回答

1条回答 默认 最新

  • dqwh0109 2015-05-04 14:04
    关注

    You do not need to use AJAX to download a file. If the content-disposition header is set, it will be downloaded and the current page will not be reloaded.

    Just add this to your php script that creates the PDF:

    header('Content-Disposition: attachment; filename=' . $MyFileName); 
    

    And instead of AJAX, use a regular anchor tag link:

    <a href="tcpdf/examples/example_061.php">Download</a>
    

    As someone noted in the comments, if you need to post you can still use a simple form with the same effect:

    <form method="POST" ACTION="tcpdf/examples/example_061.php">
    <input type="hidden" name="myPostItem" value="My POSt VALUE" />
    <input type="submit" value="download">
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵