方法一
import qs from 'qs';
export function createRequestForm(data) {
//创建form表单
var temp_form = document.createElement("form");
temp_form.action = data.url;
//如需打开新窗口,form的target属性要设置为'_blank'
temp_form.target = data.target || "_self";
temp_form.method = data.method || "post";
temp_form.style.display = "none";
//添加参数
var location = data.url.split('?')
var theParams =qs.parse(location[1],{ignoreQueryPrefix:true});
var PARAMTERS={...theParams,...data.params};
for (var item in PARAMTERS) {
var opt = document.createElement("textarea");
opt.name = item;
opt.value = PARAMTERS[item];
temp_form.appendChild(opt);
}
document.body.appendChild(temp_form);
//提交数据
temp_form.submit();
}
方法二
export function createRequestForm(data) {
//创建form表单
var temp_form = document.createElement("form");
temp_form.action = data.url;
//如需打开新窗口,form的target属性要设置为'_blank'
temp_form.target = data.target || "_self";
temp_form.method = data.method || "post";
temp_form.style.display = "none";
//添加参数
var theParams = getUrlParams(data.url);
var PARAMTERS={...theParams,...data.params};
for (var item in PARAMTERS) {
var opt = document.createElement("textarea");
opt.name = item;
opt.value = PARAMTERS[item];
temp_form.appendChild(opt);
}
document.body.appendChild(temp_form);
//提交数据
temp_form.submit();
}
export function getUrlParams(url, name) {
const arr = (url.split("?")[1] || "").split("&");
const params = {};
for (let i = 0; i < arr.length; i++) {
let data = arr[i].split("=");
if (data.length == 2) {
params[data[0]] = data[1];
}
}
if (name) {
return params[name];
}
return params;
}