javascript 如何调用WebService


服务器端代码: 
  using  System;
 
using  System.Collections;
 
using  System.Web;
 
using  System.Web.Services;
 
using  System.Web.Services.Protocols;
 
using  System.Web.Script.Services;
 
 
///   <summary>
 
///  Summary description for WebService
 
///   </summary>
 [WebService(Namespace  =   " http://tempuri.org/ " )]
 [WebServiceBinding(ConformsTo 
=  WsiProfiles.BasicProfile1_1)]
 [ScriptService]
 
public   class  WebService : System.Web.Services.WebService 
 {
     
public  WebService () 
     {
         
// Uncomment the following line if using designed components 
         
// InitializeComponent(); 
     }
 
     [WebMethod]
     
public   string  HelloWorld( string  user) 
     {
         
return   " <p style='backGround-color:red;'> " + user +   "  Hello World!</p> " ;
     }  
 }

 
客户端代码:
   < script  type ="text/javascript" >
    
// 下面的脚本调用WebService取回数据显示在webserviceDataDiv
     function  getDataFromWebService()
    {
      WebService.HelloWorld($get(
" Text1 " ).value,OnGetDataFromWebServiceCompleted, null , null );
    }
    
function  OnGetDataFromWebServiceCompleted(data)
     {
             $get(
" webserviceDataDiv " ).innerHTML = data;
     }
  
</ script >
 
  
< script  type ="text/javascript" >
     
function  getDataByServerMethod()
     {
          PageMethods.ShowDetailData($get('Text1').value,OnGetDataFromWebMethodCompleted);
     }
     
function  OnGetDataFromWebMethodCompleted(data)
     {
             $get(
" webserviceDataDiv " ).innerHTML = data;
     }
  
</ script >
 
     
< input  id ="Text1"  type ="text"  value ="me_sa"   />
     
< input  id ="Button3"  type ="button"  value ="GetDataFromWebService"  onclick ="getDataFromWebService();"   />
     
< div  id ="webserviceDataDiv" >
     
</ div >

如果你使用VS2008你可以得到这样的提示:
javascript 如何调用WebService

你可能感兴趣的:(JavaScript)