我用xmlhttprequest对象调用服务器的方法,调试的时候能够取得返回值,但是不能显示在页面上。
源程序如下:<%@ 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">
<script type="text/javascript">
    var xhr;
    function Button1_Click() {
        CreateXHR();
        if (xhr == null) {
            return false;
        }
        var val1 = document.getElementById("TextBox1").value;
        var val2 = document.getElementById("TextBox2").value;
        var queryString = "val1=" + escape(val1) + "&val2=" + escape(val2);
        xhr.open("get", "Default2.ashx?"+queryString+"&date="+escape(new Date()));
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    document.getElementById("span1").innerText=xhr.responseText;就是这个地方,用alert可以把值打印出来,但是赋值给span或者是div都不可以。
                }
            }
        }
        xhr.send(null);
        return false;
    }
    function CreateXHR() {
        if (window.XMLHttpRequest) {
            xhr = window.XMLHttpRequest;
        }
        else if (window.ActiveXObject) {
            try {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (ex) {
                    alert("Ajax is not supported by your browse");
                    xhr=null;
                }
            }
        }
        else {
            alert("Ajax is not supported by your browse");
            xhr= null;
        }
    }    
</script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
    
    </div>
    <span id="span1" xml:lang></span>
    
    </form>
</body>
</html>后台方法:
<%@ WebHandler Language="C#" Class="Default2" %>using System;
using System.Web;public class Default2 : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        HttpRequest request = context.Request;
        string param1 = request.QueryString["val1"];
        string param2 = request.QueryString["val2"];
        DateTime now;
        DateTime.TryParse(request.QueryString["date"],out now);
        HttpResponse response = context.Response;
        response.Clear();
        response.Write(string.Format("第一个参数:{0},第二个参数:{1},当前事件:{2}",param1,param2,now.ToString()));
        response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }}