XMLHttpRequest 实现无刷新更新页面数据

xmlhttp1.aspx

 

js 如下:


function createXMLHttpReq()

 {
        var xmlHttp=null;
       
        try
        {
            // Firefox, Opera 8.0+, Safari, IE7
            xmlHttp=new XMLHttpRequest();
        }
        catch(e)
        {
            // Old IE
            try
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e)
            {
                xmlHttp=null;
                alert ("Your browser does not support XMLHTTP!");
            }
        }
        return xmlHttp;

}

 

function showHint(str)
{
        if (str.length==0)
        {
            document.getElementById("txtHint").innerHTML="";
            return;
        }
       
        xmlHttp=createXMLHttpReq();
       
        url="xmlhttp2.aspx?c=" + str + "&sid=" + Math.random();
        xmlHttp.open("GET",url,false);
        xmlHttp.send(null);
        document.getElementById("txtHint").innerHTML=xmlHttp.responseText;

}

 

Body 如下:


<input type="text" id="txt1" onkeyup="showHint(this.value)" />
<div id="txtHint"></div>

 

 

xmlhttp2.aspx

 

Body中不能包含 Form

 

xmlhttp2.aspx.cs


Response.Write(Request["c"].ToString()+" Hello");

 

 

posted @ 2010-06-08 11:01  踏歌长行  阅读(479)  评论(0编辑  收藏  举报