【VUE】window.open进行post提交

window.open进行post提交

 

submitByUrl(url) {
    let name = 'tmpWinName'
    // 创建form表单
    let tempForm = document.createElement("form");
    tempForm.target = name;
    tempForm.method = "post";
    tempForm.action = url;
    // 设置对象字段到form表单的隐藏于
    const keys = Object.keys(this.catalog);
    for (let key of keys) {
        let value = this.catalog[key];
        if (typeof value != 'undefined') {
            this.formAppendChild(tempForm, key, value);
        }
    }
    // 追加临时表单到当前文档中
    document.body.appendChild(tempForm);
    window.open('about:blank', name);
    // 提交
    tempForm.submit();
    // 从文档中删除临时表单
    document.body.removeChild(tempForm);
},
formAppendChild(tempForm, name, value) {
    if (!name || !value) {
        return;
    }
    let input = document.createElement("input");
    input.type = "hidden";
    input.name = name;
    input.value = value;
    tempForm.appendChild(input);
},

 

posted @ 2026-02-03 17:28  谷粒-笔记  阅读(0)  评论(0)    收藏  举报