为什么使用AJAX后台代码只执行一次

特别 特别 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我测试一个AJAX的小例子,下面是页面文件(Default.aspx),该文件没有服务端代码
我测试时只有第一次显示时间正确,后面怎么点击按钮都不刷新时间了,
貌似Handler1.ashx中的代码只执行一次
请各位帮帮忙,是什么原因



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script type="text/javascript" language="javascript">
var xmlHttp;

function SetBList(){

xmlHttp=null;
var url = "Handler1.ashx";
createXMLHttpRequest(); // 创建xmlHttp对象
xmlHttp.onreadystatechange = handleStateChange; // 当xmlHttp状态码发生改变时,调用handleStateChage方法
xmlHttp.open("GET", url, true); // GET方法发送请求
xmlHttp.send(null);
}

function BListInitial() {

var ven1 = document.getElementById("Label1");
ven1.innerText = xmlHttp.responseText;
}


function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
BListInitial();
}
}
}

function createXMLHttpRequest() {
// IE
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
// Mozilla
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
<p>
<input id="Button1" type="button" value="button" onclick="SetBList()" /></p>
</body>
</html>



下面文件是AJAX请求的页面(Handler1.ashx)

using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace WebApplication1
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService( Namespace = "http://tempuri.org/" )]
[WebServiceBinding( ConformsTo = WsiProfiles.BasicProfile1_1 )]
public class Handler1 : IHttpHandler
{

public void ProcessRequest( HttpContext context )
{
context.Response.ContentType = "text/plain";
context.Response.Write( DateTime.Now.ToString() );
}

public bool IsReusable
{
get
{
return true;
}
}
}
}
...全文

相关推荐

评论 18
收藏 已收藏
分享
返回顶部
特别
.NET社区 62,272 成员 668,968 内容