Javascript模拟FORM以POST方式提交表单数据
如何实现Javascript模拟FORM以POST方式提交表单数据呢?
function AddDataPost(sUserId,sUserName)
{
var obj = new ActiveXObject("Microsoft.XMLHTTP");
sUserId = escape(sUserId);
sUserName = escape(sUserName);
var userInfo = "userid="+sUserId+"&username="+sUserName;
obj.open("POST","getData.asp",false);
obj.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
obj.send(userInfo);
return unescape(obj.responseText);
}
取值的时候必须用unescape()函数
<% @Language="JavaScript" %>
<%
var userId = unescape(Request.Form("userid"));
var userName = unescape(Request.Form("username"));
Response.write("ID: "+ userId +"; Name: "+ userName);
%>
第二种
document.write('<form name=myForm>');
var myForm=document.forms['myForm'];
myForm.action='';
myForm.method='POST';
myForm.submit();
function AddDataPost(sUserId,sUserName)
{
var obj = new ActiveXObject("Microsoft.XMLHTTP");
sUserId = escape(sUserId);
sUserName = escape(sUserName);
var userInfo = "userid="+sUserId+"&username="+sUserName;
obj.open("POST","getData.asp",false);
obj.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
obj.send(userInfo);
return unescape(obj.responseText);
}
取值的时候必须用unescape()函数
<% @Language="JavaScript" %>
<%
var userId = unescape(Request.Form("userid"));
var userName = unescape(Request.Form("username"));
Response.write("ID: "+ userId +"; Name: "+ userName);
%>
第二种
document.write('<form name=myForm>');
var myForm=document.forms['myForm'];
myForm.action='';
myForm.method='POST';
myForm.submit();