代码改变世界

AJAX,使用XMLHttpRequest接受客户端的提交,并发送响应

2007-03-17 09:45  ruinet  阅读(536)  评论(0编辑  收藏  举报

 

1创建XmlHttpRequest对象,读取,服务器端的reponse.xml文件内容

 


     
<script type="text/javascript">
          
var xmlHttp;
          
function createXMLHttpRequest()
          
{
              
if(window.ActiveXObject)
              
{
                  xmlHttp 
= new ActiveXObject("Microsoft.XMLHTTP");
              }

              
else if(window.XMLHttpRequest)
            
{
                xmlHttp 
= new XMLHttpRequest();
             }
         }

         
function startRequest()
        
{        
            createXMLHttpRequest();
             xmlHttp.onreadystatechange 
= handleStateChange;
            xmlHttp.open(
"GET","AjaxSend.aspx?ID=1",true);
             xmlHttp.send(
null);
         }

         
function handleStateChange()
         
{
             
if(xmlHttp.readyState==4)
             
{
                 
if(xmlHttp.status==200)
                 
{
                     alert(
"来自服务器的响应:"+xmlHttp.responseText);
                    
// document.getElementById ("Text1").innerText  =xmlHttp.responseText;
                 }

             }

        }

 
2 html代码

<input id="Button1" style="width: 85px" type="button" value="发送请求" onclick="startRequest();" />&nbsp;</div>
3 AjaxSend.aspx接受客户端的提交
 protected void Page_Load(object sender, EventArgs e)
    
{
        
string str = Request["ID"].ToString();
        
if (str=="1")
        
{
            Response .Write (
"你好,欢迎您的来到!");
        }


    }

free web counter