前端:JS使用POST方式進行跳轉


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();   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM