通过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("是否成功");来返回处理结果。

 

 

转载于:https://www.cnblogs.com/liuhaitao/archive/2009/01/06/1370414.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值