簡體   English   中英

調用php文件,該文件使用jQuery AJAX函數返回PDF

[英]Call php file which return PDF with jQuery AJAX function

我的PHP文件:example_061.php創建PDF返回,一切正常。

現在,我想使用jQuery AJAX調用該文件,以使其在屏幕上顯示,然后嘗試:

$("#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');
    },  
  });        
});

一切都很好,因此成功功能可以正常工作,我會收到警報消息,但下載或在屏幕上都看不到PDF文件。 我該怎么做?

您不需要使用AJAX來下載文件。 如果設置了content-disposition標頭,則將下載它,並且不會重新加載當前頁面。

只需將其添加到創建PDF的php腳本中:

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

而不是AJAX,請使用常規錨標記鏈接:

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

正如評論中指出的那樣,如果您需要發布,仍然可以使用具有相同效果的簡單表單:

<form method="POST" ACTION="tcpdf/examples/example_061.php">
<input type="hidden" name="myPostItem" value="My POSt VALUE" />
<input type="submit" value="download">
</form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM