【发布时间】:2020-10-05 20:53:03
【问题描述】:
我正在尝试使用 Ajax 调用从烧瓶中下载 excel。它显示响应代码为 200,但 excel 未下载,错误消息如下。
Ajax 请求:
$("#genExcel").on("点击", function() { var xhttp = new XMLHttpRequest();
// Data to post
var dataarray = {};
// Use XMLHttpRequest instead of Jquery $ajax
xhttp.onreadystatechange = function() {
var a;
if (xhttp.readyState === 4 && xhttp.status === 200) {
// Trick for making downloadable link
a = document.createElement('a');
const objectURL = window.URL.createObjectURL(xhttp.response);
a.href = objectURL
//const objectURL = URL.createObjectURL(object)
// Give filename you wish to download
a.download = "test-file.xlsx";
a.style.display = 'none';
document.body.appendChild(a);
a.click();
}
};
// Post data to URL which handles post request
xhttp.open("POST", '/genexcel');
xhttp.setRequestHeader("Content-Type", "application/json");
// You should set responseType as blob for binary responses
//xhttp.responseType = 'blob';
xhttp.send(JSON.stringify(dataarray));
});
烧瓶功能:
@app.route('/genexcel', methods=["GET", "POST"])
def createExcel():
if request.method == 'POST':
data = request.json
# process json data
return send_file(strIO, attachment_filename='test.xlsx', as_attachment=True)
错误:
1 [仅报告] 拒绝将字符串评估为 JavaScript,因为“unsafe-eval”不是以下内容安全策略指令中允许的脚本来源:“script-src * blob:”。
2 未捕获的类型错误:无法在“URL”上执行“createObjectURL”:未找到与提供的签名匹配的函数。
at XMLHttpRequest.xhttp.onreadystatechange
【问题讨论】:
标签: javascript python jquery ajax flask