欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

用JavaScript的POST方法实现页面跳转

最编程 2024-08-08 10:58:07
...
function post(URL, PARAMS) {      
    var temp = document.createElement("form");      
    temp.action = URL;      
    temp.method = "post";      
    temp.style.display = "none";      
    for (var x in PARAMS) {      
        var opt = document.createElement("textarea");      
        opt.name = x;      
        opt.value = PARAMS[x];       
        temp.appendChild(opt);      
    }      
    document.body.appendChild(temp);      
    temp.submit();      
    return temp;      
}      
     
//调用方法 如      
post('pages/statisticsJsp/excel.action', {html :prnhtml,cm1:'sdsddsd',cm2:'haha'});

post方式提交参数并下载文件:
正常情况下下载用windows.open(URL),若想用post传参可以用如下方式,生成action为URL的表格,然后利用form传参并跳转,由于跳转URL实际上是被下载的文件,所以浏览器会开始下载并保留原来页面(不确定?)。

var temp = document.createElement("form");      
temp.action = "catalogDetail.do?method=Download";      
temp.method = "post";      
temp.style.display = "none";  
var PARAMS = {
	"cata_id" : $("input[name='cata_id']").val(),
	"where" : filter_condition.where,
	"columns" : $("#gaojishaixuan_fenzu_column").val()
};
for (var x in PARAMS) {      
    var opt = document.createElement("textarea");      
    opt.name = x;      
    opt.value = PARAMS[x];       
    temp.appendChild(opt);      
}      
document.body.appendChild(temp);      
temp.submit();
$(temp).remove();