我寫了一個簡單的腳本php,使用TCPDF類來生成文件pdf。 之後,在jquery中,它返回base64中的文件,並在模態中查看。來自TCPDF腳本的AJAX返回參數
這是AJAX代碼..
$.ajax({
url: path,
type: 'post',
data: { myjson: myjson },
mimeType: 'text/plain; charset=x-user-defined',
success : function(data) {
// printPreviewBase64() is a function decodes 'data' and preview pdf in a modal
printPreviewBase64(data);
}
});
這是PHP文件..
<php
// code to generate pdf
$pdf->Output($file_name, 'I');
我怎麼能在成功也返回其它參數? 例如,我想返回:pdf路徑,日期,作者等。
Thanx!
我只是嘗試一下,但問題是「mimeType:'text/plain; charset = x-user-defined'」。所以,ajax不會返回json數據。 – perseoclub
你可以發佈應該返回json的服務器端代碼嗎? –
Thx Alani,問題在於如果使用由服務器端腳本中的json_encode()中的$ pdf-> Output($ file_name,'I');'生成的代碼,ajax會返回「parsererror」(200)。 我已經解決了,使用'$ pdf_code = base64_encode($ pdf->輸出($ file_name,'S'));'。通過這種方式,我可以返回變量爲base64!在我用js解碼之後,它就沒事了! ;) – perseoclub