日期:2014-05-18  浏览次数:20408 次

AJAX问题::不知道错误在哪,希望大哥们指点一下
我的拙见:我感觉 好像是状态有问题!可我是刚刚接触.net的 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 id="Head1" runat="server">
  <title>AJAX之加法运算示例</title>
  <script type="text/javascript">
  var xmlHttp;
  function createXMLHttpRequest()
  {
  if(window.ActiveXObject)
  {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  }
  else if(window.XMLHttpRequest)
  {
  xmlHttp = new XMLHttpRequest();
  }
  }
  function addNumber()
  {
  createXMLHttpRequest();
  var url= "ajax.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 style="text-align: center">
  <input id="num1" style="width: 99px" type="text" value="0" onkeyup="addNumber();" />+<input id="num2" style="width: 95px"
  type="text" value="0" onkeyup="addNumber();" />=<input id="result" style="width: 99px" type="text" /></div>
  </form>
</body>
</html>


ajax.ashx 文件如下:

<%@ WebHandler Language="C#" Class="ajax" %>

using System;
using System.Web;

public class ajax : 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;
  }
  }

}

------解决方案--------------------
没有问题啊(在将ajax.ashx里面的内容格式化以后..)
------解决方案--------------------
C# code
<%@ WebHandler Class="ajax" Language="C#" %>

using System;
using System.Web;

public class ajax : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "tex