我有以下ajax调用:
$.ajax({
url: 'WebService.asmx/ConvertPDF',
data: "{'section':'<html><head></head><body>Ajax html</body></html>'}",
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'JSON',
async: false,
success: function (response) {
// proceed
},
error: function () {
// fail code
}
});
和网络服务
[WebMethod(EnableSession = true)]
public void ConvertPDF(string section) {
HtmlToPdf convertor = new HtmlToPdf();
string _html = "<html><head></head><body><p>this is a test</p></body></html";
string size = "A4", orientation = "Portrait";
PdfPageSize pdfSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), size, true);
PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), orientation, true);
convertor.Options.PdfPageSize = pdfSize;
convertor.Options.PdfPageOrientation = pdfOrientation;
convertor.Options.WebPageWidth = 1024;
convertor.Options.MinPageLoadTime = 2;
convertor.Options.WebPageHeight = 0;
PdfDocument doc = convertor.ConvertHtmlString(_html, "");
doc.Save(HttpContext.Current.Response, false, "Sample.pdf");
doc.Close();
}
在本机运行服务方法它工作正常,但是当我通过 JS 按钮调用 ajax 时,我得到“线程被中止”。
关于如何绕过它的任何想法?基本上,该按钮从页面上的部分中获取 html,并(最终)将其传递给方法以输出到 PDF,本质上将 _html 变量替换为 section 参数中的内容。
谢谢