Ajax验证用户名是否存在

用Ajax访问163的通行证注册,获取返回的信息。

Register.html页面

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< html >
    
< head >
        
< title ></ title >
        
< meta  name ="GENERATOR"  content ="Microsoft Visual Studio .NET 7.1" >
        
< meta  name ="ProgId"  content ="VisualStudio.HTML" >
        
< meta  name ="Originator"  content ="Microsoft Visual Studio .NET 7.1" >
        
< script  language ="javascript"  src ="CreateXMLRequest.js" ></ script >
        
< script  language =javascript  src =CheckUserName.js ></ script >
    
</ head >
    
< body >
        
< input  type ="text"  id ="163id"  NAME ="163id"   />  
        
< input  type ="button"  value ="校验"  onclick ="AjaxCheckUserName('http://reg.163.com/register/checkssn.jsp?username=','163id','')"     ID ="Button1"  NAME ="Button1"   />
        
< div  id ="message" ></ div >
    
</ body >
</ html >

 CheckUsername.js脚本

var  xmlHttp = false ;
function  AjaxCheckUserName(url,nameDiv,httpMethod)
{
    
if ( ! httpMethod)
    {
        httpMethod
= " POST " ;
    }
    
var  regUserName = document.getElementById(nameDiv).value;
    url
+= regUserName;
    xmlHttp
= CreateXmlRequest();
    xmlHttp.onreadystatechange
= StateContent;
    xmlHttp.open(httpMethod,url,
true );
    xmlHttp.send(
null );
}

function  StateContent()
{
    
if (xmlHttp.readyState == 4 )
    {
        
if (xmlHttp.status == 200 )
        {
            MakeOperate();
        }
    }
}

function  MakeOperate()
{
    document.getElementById(
' message ' ).innerHTML = xmlHttp.responsetext;
}

CreateXMLRequest.js脚本

function  CreateXmlRequest( )
{    
    
var  xmlhttp_request  =   false ;    
    
try
    {        
            
if ( window.ActiveXObject )
            {            
                
for var  i  =   5 ; i; i --  )
                {               
                 
try
                 {                   
                     
if ( i  ==   2  )
                     {
                        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(
" Content-Type " , " gb2312 " );               

 
                     }
                        
break ;
                 }               
                     
catch (e)
                     {   
                           xmlhttp_request 
=   false ;              
                     }          
                }       
            }
            
else   if ( window.XMLHttpRequest )
                  {            
                      xmlhttp_request 
=   new  XMLHttpRequest();           
                       
if  (xmlhttp_request.overrideMimeType) 
                       {                
                            

xmlhttp_request.overrideMimeType(
' text/xml ' );            
                       }       
                  }   
    }
                   
catch (e)
                   {        
                        xmlhttp_request 
=   false ;   
                   }    
           
return  xmlhttp_request ;
}

Reader页面:

输入Rss地址,也就是Rss所对应的XML在网上的位置,比如: http://rss.sina.com.cn/book/newbooks10.xml,从返回来的XML用DOM解析显示出来
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
>
< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< title > RSS Reader </ title >
<!-- copyright Turkeycock -->
< script  type ="text/javascript" >
var  xmlHttp;

function  ShowRSS() {
var  target = document.getElementById( " targeturl " );
alert(target.value);
readRSS(target.value);
}
function  createXMLHttpRequest() {
    
if  (window.ActiveXObject) {
        xmlHttp 
=   new  ActiveXObject( " Microsoft.XMLHTTP " );
    } 
    
else   if  (window.XMLHttpRequest) {
        xmlHttp 
=   new  XMLHttpRequest();
    }
}
    
function  readRSS(url) {
    createXMLHttpRequest();
    xmlHttp.onreadystatechange 
=  handleStateChange;
    xmlHttp.open(
" GET " , url,  true );
    xmlHttp.send(
null );
}
    
function  handleStateChange() {
    
if (xmlHttp.readyState  ==   4 ) {
        
if (xmlHttp.status  ==   200 ) {
        
// document.write("<pre>"+xmlHttp.responsetext+"</pre>");
        
            clearPreviousResults();
            parseResults();
        }
    }
}

function  clearPreviousResults() {
    
var  ListBody  =  document.getElementById( " resultsTitle " );
    
while (ListBody.childNodes.length  >   0 ) {
        ListBody.removeChild(ListBody.childNodes[
0 ]);
    }
}

function  parseResults() {
    
var  results  =  xmlHttp.responseXML;
    
var  title  =   null ;
    
var  content = null ;
    
var  item  =   null ;

    
var  items  =  results.getElementsByTagName( " item " );
    
for ( var  i  =   0 ; i  <  items.length; i ++ ) {
        item 
=  items[i];
        title 
=  item.getElementsByTagName( " title " )[ 0 ].firstChild.nodeValue;
        content
= item.getElementsByTagName( " description " )[ 0 ].firstChild.nodeValue;
        addListRow(title,content);
        
    }

}


function  addListRow(title,content) {
    
var  row  =  document.createElement( " ul " );
    
var  cell  =  createCellWithText(title);
    
var  contentcell = createCellWithContent(content);
    row.appendChild(cell);
    row.appendChild(contentcell);
    
    document.getElementById(
" resultsTitle " ).appendChild(row);
}

function  createCellWithText(text) {
    
var  cell  =  document.createElement( " li " );
    
var  textNode  =  document.createTextNode(text);
    
    cell.appendChild(textNode);
    
    
return  cell;
}
function  createCellWithContent(content) {
var  cell = document.createElement( " h1 " )
 
var  textNode  =  document.createTextNode(content);
    
    cell.appendChild(textNode);
    
    
return  cell;
}

</ script >
</ head >

< body >
  
< h2 > Blog文章列表 </ h2 >
   
< input  type ="text"  id ="targeturl"  name ="textfield"   />
    
< input  type ="button"  value ="搜索"  onclick ="ShowRSS();" />     
    
< div  id ="resultsTitle" >
    
</ div >
</ body >
</ html >


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值