Here I am

Still waters run deep

一个简单的使用XMLHttpRequest获得服务器数据的例子

看网上有很多的相关资料,总结的一个Ajax入门实例。

前台代码:

定义一个XMLHttpRequest对象并使用简单的GET方法,直接从指定的url获得服务器的值。


<html>
        
<head>
            
<title>Ajax 入门</title>
            
<script type="text/javascript" language="javascript">...
                
var req;
                
function CreateRequest()
                
...{
                    
if(window.XMLHttpRequest)
                    
...{
                        req 
= new XMLHttpRequest();
                    }

                    
else
                    
...{
                        
if(window.ActiveXObject)
                        
...{
                            
try
                            
...{
                                req 
= new ActiveXObject("Msxml2.XMLHTTP");
                            }

                            
catch(msxmlException)
                            
...{
                                
try
                                
...{
                                    req 
= new ActiveXObject("Microsoft.XMLHTTP");
                                }

                                
catch(ex)
                                
...{
                                    req 
= false;
                                }

                            }

                        }
                        
                    }

                    
                    
if(!req)
                    
...{
                        alert(
"XMLHttpRequest initilized failure");
                    }

                    
                    
return req;
                }

                
                
function AjaxDemo()
                
...{
                    req 
= CreateRequest();
                    
var empno = document.getElementById("employee").value;
                    
if(empno == ""return ;
                    
var url = "Server.aspx?empno="+escape(empno);
                    req.open(
"GET",url,true);
                    req.onreadystatechange 
= StateChanged;
                    req.send(
null);
                }

                
                
function StateChanged()
                
...{
                    
if(req.readyState == 4 )
                    
...{
                        
if(req.status == 200)
                        
...{
                            alert(req.responseText);
                        }

                        
else if(req.status == 404)
                        
...{
                            alert(
"request url is not found");
                        }

                        
else if(req.status == 401 || req.status == 403)
                        
...{
                            alert(
"request url is forbidden or not authorized to visit.");
                        }

                        
else
                        
...{
                            alert(
"unexpected error!Status Code :"+req.status);
                        }

                    }
                    
                }

            
</script>
        
        
</head>
        
        
<body>
            
<div id="container" class="container">
                                       
<input id="employee" onchange="AjaxDemo();" value="" />
            
</div>
        
</body>    
</html>

 

后台代码:

后台获得传入的参数,处理完,返回【欢迎您,xxx】。

private void Page_Load(object sender, System.EventArgs e)
        
...{
            
string empno = Request.QueryString["empno"];
            ReturnValue(empno);
        }


        
public void ReturnValue(string empno)
        
...{
             
//处理.......
            Response.Write("欢迎您"+empno);
            Response.End();
        }
阅读更多
个人分类: AJAX
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭