有时候,为了隐藏页面之间传递的参数,不会用直接路由加参数的形式去跳转,而是利用post方法,
我们都知道post方法传递参数时,参数并不现实在url里面
post传参并跳转
function formPost(url,params){
var form = document.createElement("form");
form.action = url;
form.target = "_self";
form.method = "post";
form.style.dispaly = "none";
var item = {
name: '假装有参数';
value: '假装有值'
}
params = params !==undefined ? params : item;
//添加参数
for(var i in params){
var opt = document.createElement("textarea");
opt.name = params[i].name;
opt.value = params[i].value;
from.appendChild(opt);
}
document.body.appendChild(from);
from.submit();
return from;
}
//调用
var a = [{
name: "data1",
value: "value1"
},{
name: "data2",
value: "value2"
}];
formPost('http://baidu.com',a);
网友评论