完美定义点击按钮触发函数提交数据【ajax】

yuanshuai 2020-04-15

1.html代码

<button class="layui-btn layui-btn-small" style="height: 30px;line-height:1.6em;" title="导出csv" onclick="exportCSV()">
    <i class="layui-icon" style="line-height:32px;font-size: 14px;">导出csv</i>
</button>

2.javascript代码

function exportCSV (argument) {
    //导出csv
    var url = "xxx";
    $.ajax({
        url: url,
        type: "POST",
        //data: $(‘#layui-form‘).serialize(),
        data: {
            "filter_platform":$("#filter_platform").val(),
            "filter_accoount":$("#filter_accoount").val(),
            "filter_project":$("#filter_project").val(),
            "export_cvs":"export_cvs"
        },
        beforeSend: function (xhr,settings) {
            xhr.setRequestHeader("X-CSRFtoken",$.cookie("csrftoken"));
            layer.msg(‘csv生成中,请等待......‘, {icon: 4,time:600000});
        },
        success: function (data) {
            if (data.code == 0){
                window.open("/static/xxx/download/" + data.file_name);
                layer.alert(data.msg, {icon: 6});
            } else {
                layer.alert(data.msg, {icon: 7});
            }
        },
        complete: function () {
            console.info("生成csv数据执行完成......");
        },
        error: function (data) {
            console.info("生成csv数据执行出错......");
        }
    });
}

相关推荐

84593973 / 0评论 2020-06-14