【发布时间】:2016-07-15 10:00:28
【问题描述】:
我的网站上有一个表格,用户需要填写该表格才能生成 PDF 文件。如果表单中有错误,服务器将使用带有错误列表的 json 对象进行响应,否则将使用 PDF 文件(作为字符串)进行响应。我正在尝试在 Magnific-Popup (https://www.npmjs.com/package/magnific-popup) 中显示该 PDF 文件。
$('.preview-button').on('click', function (e) {
event.preventDefault();
var theLink = $(this);
var formElement = theLink.closest("form");
$.ajax({
url: theLink.attr('href'),
method: 'GET',
data: formElement.serialize(),
success: function (response, textStatus, jqXHR) {
var contentType = jqXHR.getResponseHeader("content-type") || "";
if(contentType.indexOf('pdf') > -1){
// How can I tell to magnific popup to display the response as PDF?
$.magnificPopup.open({
// tell magnific popup to use the response...
type: 'iframe',
closeOnBgClick: false
});
}
else{
if (response.hasOwnProperty('errors')){
// Form has error, show them
alert(response.errors);
}
}
}
});
});
【问题讨论】:
标签: javascript ajax pdf magnific-popup