zoukankan      html  css  js  c++  java
  • 通过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("是否成功");来返回处理结果。

  • 相关阅读:
    多线程批量插入数据到数据库
    分分钟搞定redis
    Eclipse中JS文件红叉处理
    springmvc基础篇—处理图片静态资源文件
    springmvc基础篇—使用注解方式为前台提供数据
    springmvc基础篇—拆分配置文件
    springmvc基础篇—通过注解的方式去配置项目
    springmvc基础篇—修改默认的配置文件名称及位置
    springmvc基础篇—掌握三种控制器
    springmvc基础篇—掌握三种处理器
  • 原文地址:https://www.cnblogs.com/liuhaitao/p/1370414.html
Copyright © 2011-2022 走看看