欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用post打开新窗口

程序员文章站 2022-04-04 23:22:14
...

代码片段

//div 中的file 上传框
<input id="xxx" type="text" placeholder="请输入内容"/>
var text = $("#xxx").val();

//用法
function a(){
	var url = ctx + "/xxx/xxx/xxx?_csrf="+csrf;
	var text = $("#xxx").val();
	var keys = new Array("text","aaa","bbb");
	var values = new Array(text,aaa,bbb);
	openWindowWithPost(url, keys, values);
}

/**
 * post提交表单
 */
function openWindowWithPost(url,keys,values){
	var newWindow = window.open(url);
	if (!newWindow){
		return false;
	}
	var html = "";
	html += "<html><head></head><body><form id='formid' method='post' action='" + url + "' accept-charset='UTF-8' 'document.charset='UTF-8''>";
	for(var i=0;i<keys.length;i++){
		var key = keys[i];
		var value = values[i];
		if (key && value){
			html += "<input type='hidden' name='" + key + "' value='" + value + "'/>";
		}
	}
	html += "</form><script type='text/javascript'>document.getElementById('formid').submit();";
	html += "<\/script></body></html>".toString().replace(/^.+?\*|\\(?=\/)|\*.+?$/gi, "");
	newWindow.document.write(html);
	return newWindow;
} 

//后台代码
@RequestMapping("xxx")
public ModelAndView xxx(String text, String aaa, String bbb) {
	ModelAndView model = new ModelAndView();
	try {
		text = URLDecoder.decode(text, "utf-8");  
	} catch (UnsupportedEncodingException e) {
		e.printStackTrace();
	} 
	model.addObject("text", text);
	model.addObject("aaa", aaa);
	model.addObject("bbb", bbb);
	model.setViewName(MODELPATH + "xxx");
	return model;
}