xmlhttp调用后台

代码
    <script language="javascript" type="text/javascript">
         
        
function CallServer(url) {

//            xml = new ActiveXObject("Microsoft.XMLHTTP");
//
            var post = "";
//
            xml.open("POST", url, false);
//
            xml.setrequestheader("content-length", post.length);
//
            xml.setrequestheader("content-type", "application/x-www-form-urlencoded");
//
            xml.send(post);
//
            var res = unescape(xml.responseText);
//
            return res

            xmlHttp 
= GetXmlHttpObject();
            
if (!xmlHttp || xmlHttp == null) {
                alert(
'browser does not support http request')
                
return;
            }
            xmlHttp.open(
"GET", url, false);
            xmlHttp.send(
null);
            
var res = unescape(xmlHttp.responseText);
            
return res
        }

        
function CallServerAsy(url, ID) 
        {
            xmlHttp 
= GetXmlHttpObject();
            
if (!xmlHttp || xmlHttp == null) {
                alert(
'browser does not support http request')
                
return;
            }

           
//xmlHttp.onreadystatechange = stateChanged;
           //xmlHttp.onreadystatechange = stateChanged(ID);
            xmlHttp.onreadystatechange =  function() { stateChanged(ID) }; 
            xmlHttp.open(
"GET", url, true);
            xmlHttp.send(
null);
          
        }
    
    
        
var xmlHttp;
        
function showHint(str) 
        {
            
if (str.length == 0) {
                document.getElementById(
"TextBoxHint").innerHTML = "";
                
return;
            }

            
var url = "LastNameLookup.aspx"
            url 
= url + "?q" + str;

            document.getElementById(
"TextBoxHint").innerHTML = "正在处理……";

            
//同步调用
            //document.getElementById("TextBoxHint").innerHTML = CallServer(url);
            
            
//异步调用
            CallServerAsy(url, "TextBoxHint")
        }

        
function stateChanged(ID) 
        {
            
var OK = 200;
            
if ((xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
                
&& xmlHttp.status == OK) {      
                document.getElementById(ID).innerHTML 
= xmlHttp.responseText;            
            }
        }

        
function GetXmlHttpObject(hander) {
            
var objXMLHttp = null;
            
if (window.XMLHttpRequest) {
                
try {
              
                   objXMLHttp 
= new XMLHttpRequest();
                }
                
catch(e)
                {
                }
            }
            
else if(window.ActiveXObject)
            {
                
try {
                   
                   objXMLHttp 
= new ActiveXObject("Microsoft.XMLHTTP");
                   
               }
               
catch(e)
               {
               }
            }
            
return objXMLHttp;
        }
    
</script>
 
代码
<div>
       
<h1>Lookup</h1>
       
       
<b>Last name:</b><asp:TextBox ID="TextBox1" runat="server" onkeyup="showHint(this.value)"></asp:TextBox>
       
       
<p><b><i>Names: </i></b>&nbsp;<span id="TextBoxHint"></span></p>
       
    
</div>

 

LastNameLookup.aspx后台:

 

代码
                System.Threading.Thread.Sleep(10000);
                
string queryLastName = Request.QueryString.Get(0).ToString();
                
string returnString = "";
                returnString 
= "<select size=1>";
                returnString 
+= "<option>111</option>";
                returnString 
+= "<option>222</option>";
                returnString 
+= "<option>333</option>";
                returnString 
+= "<option>444</option>";
                returnString 
+= "<option>555</option>";
                returnString 
+= "</select>";
            
    Response.Write(returnString);
                Response.End();

 

注意点:open方法指定get还是post,同步还是异步,异步回调方法的指定和参数传递

       后台的Response.End(),否则会把所有网页都返回

 

 

posted on 2010-04-01 17:47  优雅小猪  阅读(309)  评论(0编辑  收藏  举报

导航