ASP.net Ajax 调用Web Service

HTML:

 

代码
< asp:ScriptManager  runat ="server"  ID ="sm" >
     
< Services >
       
< asp:ServiceReference  Path ="~/validateUserName.asmx"   />
     
</ Services >
    
</ asp:ScriptManager >
    
< div >
       
< input  type ="text"  id ="userName"   onchange ="test();" />
       
&nbsp; < span  id ="msg" ></ span >
    
</ div >

 

 

JS:

 

代码
< script type = " text/javascript " >
        
function  test() {
            requestService 
=  JQueryProj.validateUserName.GetUser(document.getElementById( " userName " ).value, validate);
        }
        
function  validate(result) {
            
            
if  (result  ==   " false " ) {
                msg.innerHTML 
=   " 用户名已存在! " ;
            }
            
else  {
                msg.innerHTML 
=   " 用户名可用! " ;
                
            }
        }
    
< / script>

 

 

Web Service

 

代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.Services;
using  System.Web.Script.Services;

namespace  JQueryProj
{
    
///   <summary>
    
///  validateUserName 的摘要说明
    
///   </summary>
    [WebService(Namespace  =   " http://tempuri.org/ " )]
    [WebServiceBinding(ConformsTo 
=  WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(
false )]
    
//  若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    [ScriptService]
    
public   class  validateUserName : System.Web.Services.WebService
    {

        [WebMethod]
        
public   string  GetUser( string  userName)
        {
            
if  (userName  ==   " admin " )
            {
                
return   false .ToString().ToLower();
            }
            
else
            {
                
return   true .ToString().ToLower();
                
            }
        }
    }
}

 

 

你可能感兴趣的:(web Service)