【发布时间】:2016-03-29 20:44:31
【问题描述】:
我正在使用带有 Django 的 Reportlab 来生成在客户端计算的数据的 pdf。
选择 Ajax 是因为客户端有重要的数据要传输以生成 pdf。
$.ajax({
type:'POST',
url:document.URL + '/export',
data: JSON.stringify(data),
dataType:'json',
contentType:'application/pdf',
success:function(data){
// Question 1. What should I insert here to download pdf?
},
error:function(e){
...
}
});
这里是 view.py
def export(request, *args, **kwargs):
// Perform Ajax check
...
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="essay.pdf"'
p = canvas.Canvas(response, bottomup=0)
data = json.loads(request.body)
p.drawString(100, 100, "Hello world.")
p.showPage()
p.save()
return response
问题 2. 我无法让 ajax 成功完成其请求,只能调用错误回调。这个问题中提到了同样的问题
但没有人回答。我错过了什么吗?
【问题讨论】:
标签: jquery ajax django reportlab