如何使用 JS 以 POST 方法提交数据

我们通常使用 window.open() 方法在 JS 中打开一个窗口并提交数据。

这种方法提交的数据为 GET 方法。

如果希望使用 POST 方法,可以使用下面的方法。[code]function OpenWindowWithPost(url, windowoption, name, params) {
var form = document.createElement(“form”);
form.setAttribute(“method”, “post”);
form.setAttribute(“action”, url);
form.setAttribute(“target”, name);

for ( var i in params) {
	if (params.hasOwnProperty(i)) {
		var input = document.createElement('input');
		input.type = 'hidden';
		input.name = i;
		input.value = params[i];
		form.appendChild(input);
	}
}

document.body.appendChild(form);

//note I am using a post.htm page since I did not want to make double request to the page 
//it might have some Page_Load call which might screw things up.
window.open("post.htm", name, windowoption);

form.submit();

document.body.removeChild(form);

}

function NewFile() {
var param = {
‘uid’ : ‘1234’
};
OpenWindowWithPost(
“NewFile.aspx”,
“width=730,height=345,left=100,top=100,resizable=yes,scrollbars=yes”,
“NewFile”, param);
}[/code]