简单实现Ajax

1.ajax.js

var XMLHttpReq=false;
function creatXMLHttpRequst()
{
 if(window.XMLHttpRequest)
 {
  XMLHttpReq=new XMLHttpRequest();
 }
 else if(ActiveXObject)
 {
  try
  {
   XMLHttpReq=new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch(e)
  {
   try
   {
    XMLHttpReq=new ActiveXObject("Microsoft.XMLHTTP");
   }
   catch(e)
   {
   }
  }
 }
}
function sendRequest(url)
{
 creatXMLHttpRequst();
 XMLHttpReq.open("GET",url,true);
 XMLHttpReq.onreadystatechange=processResponse;
 XMLHttpReq.send(null);
}
function processResponse()
{
 if(XMLHttpReq.readyState==4)
 {
  if(XMLHttpReq.status==200)
  {
   //var res=XMLHttpReq.responseXML.getELementsByTagName("res")[0].firstChild.data;
   //var res="aaa";responseXML.getElementsByTagName("name"
   //var res=XMLHttpReq.responseXML.getElementsByTagName("Name");
   //window.alert(res);
   //var xmlResult = XMLHttpReq.responseText;
   
   var tt=XMLHttpReq.responseText;
   window.alert(tt);
   //window.alert(xmlResult.xml);
   //document.write(xmlResult.xml);
   parent.document.write(tt);
  }
  else
  {
   window.alert("fjjj");
  }
 }
}

2.ajax.aspx
 function userCheck() { var un=document.Form1.uname.value; var psw=document.Form1.psw.value; if(un=="") { window.alert("用户名不能为空"); document.Form1.uname.focus(); return false; } else { sendRequest('login.aspx?uname='+un+'&psw='+psw); } }

3.login.aspx

// 在此处放置用户代码以初始化页面
   string str_name,str_psw;
   str_name=Request.QueryString["uname"].ToString();
   str_psw=Request.QueryString["psw"].ToString();
   if(str_name=="a" && str_psw=="b")
   {
    //Response.Write("zmmzmmzmmzmmzmmm");
//    txtResult = "<?xml version='1.0'?><Name>zmm</Name>";
    Response.Clear();  
//    Response.Charset = "big5";
    //Response.ContentType = "text/xml";
    //Response.Expires = 0;
    Response.Write("zmmzmmzmmzmmzmmm");
//    Response.Flush();  
     Response.End();
   }
   else
   {
    Response.Write("mmmmmmmmmmmmmmm");
//    txtResult = "<?xml version='1.0'?><Name>mxh</Name>";
//    Response.Clear();  
//    Response.Charset = "big5";
//    Response.ContentType = "text/xml";
//    Response.Expires = 0;
//    Response.Write(txtResult);
//    Response.Flush();  
//     Response.End();
   }
posted @ 2006-11-26 10:07  kuailewangzi1212  阅读(354)  评论(0编辑  收藏  举报