通过ajax请求获取返回的文件url
1、html
<button type="button" class="btn btn-primary" id="download_search">下载</button>
2、js
$(function(){
//点击下载按钮事件
$('#download_search').click(function() {
$.ajax({
type: "get",
async: false,
url: '/list',
data: {
xx: yy
},
success: function(result) { // result为返回的数据
location.href = result.url;
}
});
});
});

本文介绍了一种利用Ajax请求获取文件URL并实现自动下载的方法。通过html中的按钮触发JavaScript的Ajax请求,设置请求类型为GET,请求地址为'/list',并在成功回调中通过location.href跳转到返回的文件URL,实现文件的自动下载。
489

被折叠的 条评论
为什么被折叠?



