第一次用AJAX,在网上找了个例子.调试不出来,各位帮帮忙~~~ Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" language="javascript"> var xmlHttp;
function createXMLHttpRequst() {
try {
xmlHttp = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
xmlHttp = false;
}
}
function addNumber()
{
createXMLHttpRequst();
var url = "Handler.ashx?num1="+document.getElementById("num1").value+"&num2="+document.getElementById("num2").value;
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=showResult;
xmlHttp.send(null);
}
function showResult()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
document.getElementById("result").value=xmlHttp.responseText;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="num1" type="text" style="width:98px" onkeyup="addNumber();" value="0"/>+<input id="num2" type="text" style="width:98px" onkeyup="addNumber();" value="0"/>=
<input id="result" style="width:98px" type="text" />
</div>
</form>
</body>
</html>
Handler.ashx<%@ WebHandler Language="C#" Class="Handler" %>using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
int result = Convert.ToInt32(context.Request.QueryString["num1"]) + Convert.ToInt32(context.Request.QueryString["num2"]);
context.Response.Write(result);
}
public bool IsReusable {
get {
return false;
}
}}
麻烦各位帮忙看看问题在哪??谢谢先>>>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" language="javascript"> var xmlHttp;
function createXMLHttpRequst() {
try {
xmlHttp = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
xmlHttp = false;
}
}
function addNumber()
{
createXMLHttpRequst();
var url = "Handler.ashx?num1="+document.getElementById("num1").value+"&num2="+document.getElementById("num2").value;
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=showResult;
xmlHttp.send(null);
}
function showResult()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
document.getElementById("result").value=xmlHttp.responseText;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="num1" type="text" style="width:98px" onkeyup="addNumber();" value="0"/>+<input id="num2" type="text" style="width:98px" onkeyup="addNumber();" value="0"/>=
<input id="result" style="width:98px" type="text" />
</div>
</form>
</body>
</html>
Handler.ashx<%@ WebHandler Language="C#" Class="Handler" %>using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
int result = Convert.ToInt32(context.Request.QueryString["num1"]) + Convert.ToInt32(context.Request.QueryString["num2"]);
context.Response.Write(result);
}
public bool IsReusable {
get {
return false;
}
}}
麻烦各位帮忙看看问题在哪??谢谢先>>>
{
xmlHttp = false;
try
{
xmlHttp = new XMLHttpRequest();
}
catch (trymicrosoft)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (othermicrosoft)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (failed)
{
xmlHttp = false;
}
}
}
if (!xmlHttp)
{
alert("初始化XMLHttpRequest失败!");
}
}