javascript写的ajax请求

本文介绍了一种使用Ajax技术与ASP.NET结合的方法,通过简单的HTML页面与JavaScript代码实现了按钮点击触发异步请求的功能。具体实现包括创建XMLHttpRequest对象、设置请求方式及URL,并在回调函数中处理服务器响应。

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

 1 <html xmlns="http://www.w3.org/1999/xhtml">
 2 <head runat="server">
 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 4     <title></title>
 5     <script type="text/javascript">
 6         onload = function () {
 7             window.document.getElementById("Button1").onclick = function () {
 8                 var xhr = null
 9                 if (XMLHttpRequest) {
10                     xhr = new XMLHttpRequest();
11                 } else {
12                     xhr = new ActiveXObject("Microsoft.XMLHTTP");//兼容IE6,IE5
13                 }
14                 xhr.open("get", "Handler2.ashx?a=bbb",true);
15                 xhr.send();
16                 xhr.onreadystatechange = function () {//回调函数
17                     if(xhr.readyState==4){
18                         if (xhr.status == 200) {
19                             alert(xhr.responseText);
20                         }
21                     }
22                 };
23 
24             }
25             
26         };
27 
28     </script>
29 </head>
30 <body>
31     <form id="form1" runat="server">
32     <div>
33     
34         <asp:Button ID="Button1" runat="server" Text="Button" />
35     
36     </div>
37     </form>
38 </body>
39 </html>

 

转载于:https://www.cnblogs.com/chao2014/p/4960542.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值