使用 ajax 异步调用数据

本文介绍了一种使用Ajax技术实现网页上异步数据加载的方法。通过一个具体实例展示了如何编写Ajax脚本来请求服务器端的数据,并将这些数据动态地显示在网页中。此外还涉及了服务器端代码如何接收并响应Ajax请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ajax 脚本

<script type="text/javascript" >
       function show(page)
        {
          var xmlhttp;
          try
          {
            xmlhttp=new XMLHttpRequest();
          }
          catch(e)
          {
             xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
          xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4)
          {
            if (xmlhttp.status==200)
            {
                var data=xmlhttp.responseText;        
                document.getElementById("divInfor").innerHTML=unescape(data);   //divInfor 需要显示异步调用数据的地方
            }
          }
        }
            xmlhttp.open("post", "showInfor.aspx", true);    //showInfor.aspx 写异步调用数据的地方
            xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
            xmlhttp.send("lx=all&page="+escape(page));       //需要传递的参数
        }    
    </script>

 

HTML页面

<form id="form1" runat="server">
    <div>
      如何使用ajax来实现异步调用数据
    </div>
    <div><input type="button"  value="显示ajax内容"  onclick="show('5')"  /></div>
    <br />
    <div id="divInfor"></div>
</form>

 

异步调用页面代码   showInfor.aspx

public partial class showInfor : System.Web.UI.Page
{
    protected string strType = "";
    protected string strPage = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        strType = Convert.ToString(Request.Form["lx"]);              //参数调用是用Request.Form  而不是Request.QueryString 
        strPage = Convert.ToString(Request.Form["page"]);

        Response.Write("类型为:" + strType + "<br /> 页面为:" + strPage);
    }
}

 

转载于:https://www.cnblogs.com/enamorbreeze/p/6283664.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值