通过XMLHttpRequest发送异步请求
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript" language="javascript">
function ajaxSubmit(){
var xmlhttp;
try{
xmlhttp=new XMLHttpRequest();
}catch(e){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//创建请求结果处理程序
xmlhttp.onreadystatechange=function(){
if (4==xmlhttp.readyState){
if (200==xmlhttp.status){
var date=xmlhttp.responseText;
//addToList(date);
alert(date);
}else{
alert("error");
}
}
}
//打开连接,true表示异步提交
xmlhttp.open("post", "http://localhost:3248/RisingMsgSite/Activity/GetComment.aspx", true);
//当方法为post时需要如下设置http头
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
//发送数据
var strEmail = "Email="+document.getElementById("Text1").value+"&Sbody="+document.getElementById("Text2").value;
//alert(strEmail);
xmlhttp.send(strEmail);
//xmlhttp.send(strsbody);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Email: <input id="Text1" type="text" name="Email"/>Sbody:<input id="Text2" type="text" name="Sbody"/>
<input type="button" ID="btn" name="btn" value="OK" onclick="ajaxSubmit();"/>
</div>
</form>
</body>
</html>
GetComment.aspx的cs文件中通过
string strEmail = Request["Email"].ToString();
string strSbody =Request["Sbody"].ToString();
来接收数据,通过 Response.Write("是否成功");来返回处理结果。