当前位置:编程学习 > XML/UML >>

ajax:利用XMLHttpRequest建立到服务器的连接并处理返回信息

<html>
<head>
<script language="javascript" type="text/javascript">
   function ajaxFunction()
   {
   var xmlHttp;
   try
   {
   // Firefox,Opera 8.0+,Safari
   xmlHttp=new XMLHttpRequest();
   }
   catch (e)
   {
   // Internet Explorer
   try
   {
   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
   }
   catch (e)
   {
   try
   {
   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
   catch (e)
   {
   alert("您的浏览器不支持AJAX!");
   return false;
   }
   }
   }
 
/*以上浏览器类型的判定项也可等价为: www.zzzyk.com
 
try{
 
  if( window.ActiveXObject ){
 
  for( var i = 5; i; i-- ){
 
  try{
 
  if( i == 2 ){ //IE
 
  xmlhttp_request = new ActiveXObject( "Microsoft.XMLHTTP" ); }
 
  else{
 
  xmlhttp_request = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
 
  xmlhttp_request.setRequestHeader("Content-Type","text/xml");
 
  xmlhttp_request.setRequestHeader("Charset","gb2312"); }
 
  break;}
 
  catch(e){
 
  xmlhttp_request = false; } } }
 
  else if( window.XMLHttpRequest ) //Firefox,Opera 8.0+,Safari
 
 
  { xmlhttp_request = new XMLHttpRequest();
 
  if (xmlhttp_request.overrideMimeType)
 
  { xmlhttp_request.overrideMimeType('text/xml'); } } }
 
  catch(e){ xmlhttp_request = false; }
 
*/
xmlHttp.onreadystatechange=function()
   {
  if(xmlHttp.readyState==4&&xmlHttp.status==200)//xmlHttp.status标示HTTP连接的状态
   {
   document.myForm.time.value=xmlHttp.responseText;
   }
   }
         xmlHttp.open("GET","http://",true);
   xmlHttp.send(null);
if(document.myForm.username.value.length==0)
 
alert(xmlHttp.responseText);
   }
   </script>
</head>
   <body>
   <form name="myForm" onSubmit="return ajaxFunction()" >
   用户:<input type="text" name="username" >
   时间:<input type="text" name="time" >
 <input type="submit" name="button" value="提交">
   </form>
   </body>
   </html>
 
补充:Web开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,