为什么使用AJAX后台代码只执行一次
我测试一个AJAX的小例子,下面是页面文件(Default.aspx),该文件没有服务端代码
我测试时只有第一次显示时间正确,后面怎么点击按钮都不刷新时间了,
貌似Handler1.ashx中的代码只执行一次
请各位帮帮忙,是什么原因
下面文件是AJAX请求的页面(Handler1.ashx)
我测试时只有第一次显示时间正确,后面怎么点击按钮都不刷新时间了,
貌似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;
}
}
}
}
...全文
相关推荐
JS实现定时任务每隔N秒请求后台setInterval定时和ajax请求问题
主要介绍了JS实现定时任务每隔N秒请求后台setInterval定时和ajax请求 的相关资料,需要的朋友可以参考下
PHP ajax 异步执行不等待执行结果的处理方法
主要介绍了PHP ajax 异步执行不等待执行结果的处理方法,本文直接给出实现代码,需要的朋友可以参考下
Ajax实现定时刷新,获取后台数据实现技术
Ajax实现定时刷新,获取后台数据实现技术
jquery+ajax每秒向后台发送请求数据然后返回页面的代码
jquery+ajax每秒向后台发送请求数据然后返回页面(包括jquery页面加载完毕才执行方法)
Jquery公告滚动+AJAX后台得到数据
ajax得到值,用JQUERY绑定给对应的UL.利用JQUERY的动画来实现他们的滚动公告。


