[英]Getting Array keys in POST method using POSTMAN in Python 3 Flask-Restful API
[英]Download Excel using AJAX (POST METHOD) with Flask-Restful Python
我正在尝试使用 flask 和 Ajax 下载 excel,我使用了 POST 方法,因为有很多数据要传递 (JSON)。 但是当返回 excel 时出现错误“方法不允许”。但是当我使用 GET METHOD 时,它可以返回我的 excel 文件,但我无法在服务器中获取我的 JSON 数据。
这是我使用 POST 时的代码:
def post(self):
output = io.BytesIO()
workbook = xlsxwriter.Workbook(output)
worksheet = workbook.add_worksheet()
filename = 'excel'+".xls"
worksheet.merge_range('A2:E2', 'WORK!!')
workbook.close()
response = make_response(output.getvalue())
response.headers['Content-Type'] = "application/x-xlsx"
response.headers["Cache-Control"] = "no-cache"
response.headers["Content-Disposition"] = "attachment; filename="+filename
return response
这是我的 ajax 代码:
$.ajax({
url: '/downloadFile',
type: 'POST',
data: JSON.stringify({
data1: data1,
data2: data2
}),
dataType: "JSON",
contentType: "application/json; charset=utf-8",
traditional: true,
success: function (response) {
alert('ok')
window.location = this.url;}})
我不能使用 AJAX 做这件事,但我尝试使用 XMLHttpRequest 并且它有效。
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
var a;
if (xhttp.readyState === 4 && xhttp.status === 200) {
a = document.createElement('a');
a.href = window.URL.createObjectURL(xhttp.response);
a.download = "-Download"+full_date+".xlsx"
a.style.display = 'none';
document.body.appendChild(a);
a.click();
}
};
xhttp.open("POST", '/downloadFile/');
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.responseType = 'blob';
xhttp.send(JSON.stringify({ data1: data1, data2: data2}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.