我的网站上有一个用户需要填写的表格,以便生成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);
}
}
}
});
});
这对于遇到相同问题的人可能很有用:我决定不将pdf文件作为响应发送,而是决定在服务器中生成一个临时pdf文件,并将该文件的路径作为响应发送。然后,我们可以告诉magnificPopup将其显示在iframe中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句