Ajax 获取数据代码

无刷新获取字符串:

Html网页中:

<script>
//定义异步对象
var xmlHttp;
//封装方法
function CreateXMLHTTP() {
   try {
            xmlHttp = new XMLHttpRequest();
        } catch (e) {
     xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
}
window.onload = function () {
  document.getElementById("name").onclick = function () 
     //创建异步对象
  CreateXMLHTTP();
     //通过事件来接受服务器返回
  xmlHttp.onreadystatechange = function () {

   //判断状态
  if (xmlHttp.readyState==4&& xmlHttp.status==200) {
    //获取你要的数据
    var data = xmlHttp.responseText;
    document.getElementById("d1").innerText = data;
      }
    }
     //设置将要访问的服务器地址和方式
    xmlHttp.open("get", "Data.ashx");

    //发送异步请求
    xmlHttp.send();
  }
}
</script>
</head>
<body>
<div id="d1"></div>
<input type="button" id="name" value="获取数据" />
</body>

提供数据页:Data.ashx中:

public void ProcessRequest (HttpContext context) {
  context.Response.ContentType = "text/plain";
  //线程  过两秒在获取数据
  System.Threading.Thread.Sleep(2000);
    //你提供的数据--》字符串
  context.Response.Write("这你要的数据");
}

-----------------------------------------------------------------------------------------------------------

无刷新获取一张表,然会在静态网页中无刷新删除数据

Html网页中:

<body>
<input type="button" id="name" value="获取表" />
<div id="d1"></div>
</body>
</html>
<script>
var xmlHttp;
function CreateHTTP() {
try {
  xmlHttp = new XMLHttpRequest();
  } catch (e) {
  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
}
//把相同的代码封装下
function Common() {
    CreateHTTP();
    xmlHttp.onreadystatechange = function () {
  if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {

    //接受数据--》一张表的字符串
    var table = xmlHttp.responseText;
    //显示在网页中
    document.getElementById("d1").innerHTML = table;
    }
   }
}
document.getElementById("name").onclick = function () {
    Common();
    xmlHttp.open("get", "Table.ashx");
    xmlHttp.send();
}
//删除链接的脚本  
function dd(id) {   //id传过来的删除的ID
    Common();
    xmlHttp.open("get", "Table.ashx?id=" + id);
    xmlHttp.send()
}
</script>

提供数据页:Table.ashx中

public void ProcessRequest (HttpContext context) {
context.Response.ContentType
= "text/plain"; //如果没有传值代表获取一张表 if (context.Request.QueryString["id"]!=null) {    //得到需要删除的ID   int id = int.Parse(context.Request.QueryString["id"].ToString());   int r = SQLHelper.ExecuteNonQuery("delete Users where ID=@0", CommandType.Text, id);   if (r>0)   {    Common(context);   } }   else   {     Common(context);   } } //获取一张表的字符串 public void Common(HttpContext context) {   SqlDataReader dr = SQLHelper.ExecuteReader("select * from Users", CommandType.Text);   string newTable = "<table cellspacing='0' rules='all' border='1' id='GridView1' style='border-collapse:collapse;'><tr><th scope='col'>ID</th>”+   “<th scope='col'>UserName</th><th scope='col'>Password</th><th scope='col'>Sex</th><th scope='col'>AdminID</th><th scope='col'>删除“+”</th></tr>"; //通过读取行的数据来补全一张表   while (dr.Read())   {   string sex = "checked='checke'";   if (dr[4].ToString().ToLower() == "false")   {    sex = "";   }   newTable += "<tr><td>" + dr[0] + "</td><td>" + dr[1] + "</td><td>" + dr[2] + "</td><td><span class='aspNetDisabled'><input type='checkbox'"
+ sex + " disabled='disabled' /></span></td><td>" + dr[3] + "</td><td>"+ //javascript:dd("+dr[0]+") 这是个脚本在静态网页中实现   "<a href='javascript:dd(" + dr[0] + ")'>删除</a></td></tr>";   }   dr.Close();   newTable += "</table>";   context.Response.Write(newTable); }
原文地址:https://www.cnblogs.com/Sea1ee/p/5907625.html