访问一个url并且携带一个表单通过post传递给网站
参数解析:url【跳转地址】;data【需要传输的数据,JSON格式】;name【打开新窗口的标签名】
openPostWindow(url, data, name) {
var tempForm = document.createElement("form");
tempForm.id = "tempForm1";
tempForm.method = "post";
tempForm.action = url;
tempForm.target = name; // _blank - URL加载到一个新的窗口,需要打开新弹窗的时候就传name值
for (let item in data) {
let hideInput = document.createElement("input");
hideInput.type = "hidden";
hideInput.name = item;
hideInput.value = data[item];
tempForm.appendChild(hideInput);
}
if (document.all) { // 兼容不同浏览器
tempForm.attachEvent("onsubmit", function () { }); //IE
} else {
tempForm.addEventListener("submit", function () { }, false); //firefox
}
document.body.appendChild(tempForm);
if (document.all) { // 兼容不同浏览器
tempForm.fireEvent("onsubmit");
} else {
tempForm.dispatchEvent(new Event("submit"));
}
tempForm.submit();
document.body.removeChild(tempForm);
},
网上有类似的方法和功能,这里总结起来方便使用,并且变量都是传入而不是自定义,方便在任何场景使用。主要是针对一些公司老项目和安全系数较高的系统使用。