客户端直接调用WebServices

<% @ Page Language = " C# "  AutoEventWireup = " true "   CodeFile = " Default1.aspx.cs "  Inherits = " _Default "   %>
<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head runat = " server " >
    
< title > JS 呼叫 WebService </ title >
    
< script language = javascript >
    function RequestByGet(Namestr)
    
{
        var Name
=document.form1.txtName.value;
        var xmlhttp
=new ActiveXObject("Microsoft.XMLHTTP");
        var URL
="http://localhost/AspWebSite/WebService.asmx/SayHelloTo?Name=HOOK";
        xmlhttp.Open(
"GET",URL,false);
        xmlhttp.SetRequestHeader(
"Content-Type","text/xml;charset=utf-8");
        xmlhttp.SetRequestHeader(
"SOAPAction","http://tempuri.org/SayHelloTo");
        xmlhttp.Send(Namestr);
        alert(xmlhttp.status);
        
if(xmlhttp.status==200)
        
{
            document.write(xmlhttp.responseText);
        }
    
            
      }

      
      
      function RequestByGet1()
    
{
        var xmlhttp
=new ActiveXObject("Microsoft.XMLHTTP");
        var URL
="http://localhost/AspWebSite/WebService.asmx/SayHelloTo?Name=HOOK";
        xmlhttp.Open(
"GET",URL,false);
        xmlhttp.Send();
        alert(xmlhttp.status);
        
if(xmlhttp.status==200)
        
{
            document.write(xmlhttp.responseText);
        }
    
            
      }

       
function RequestByPost(value)
{
var data;
data 
='<?xml version="1.0" encoding="utf-8"?>'
data 
= data + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
data 
= data + '<soap:Body>'
data 
= data + '<SayHelloTo xmlns="http://tempuri.org/">'
data 
= data + '<Name>'+value+'</Name>'
data 
= data + '</SayHelloTo>'
data 
= data + '</soap:Body>'
data 
= data + '</soap:Envelope>'

var xmlhttp 
= new ActiveXObject("Microsoft.XMLHTTP"); 
var URL
="http://localhost/AspWebSite/WebService.asmx";
xmlhttp.Open(
"POST",URL, false); 
xmlhttp.SetRequestHeader (
"Content-Type","text/xml; charset=gb2312"); 
xmlhttp.SetRequestHeader (
"SOAPAction","http://tempuri.org/SayHelloTo"); 
xmlhttp.Send(data); 
document.write( xmlhttp.responseText); 

}

</ script >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
    
< div >
        
< br  />
        
< input id = " txtName "  type = " text "   />< br  />
        
< br  />
        
< input id = " btnGet "  type = " button "  value = " Get "   onclick = " RequestByGet1('Ya') " />
        
< input id = " btnPost "  type = " button "  value = " Post "   onclick = " RequestByPost('Ming') "   /></ div >
    
</ form >
</ body >
</ html >

 WebServices:

 

using  System;
using  System.Web;
using  System.Collections;
using  System.Web.Services;
using  System.Web.Services.Protocols;


/// <summary>
/// Summary description for WebService
/// </summary>

[WebService(Namespace  =   " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo 
=  WsiProfiles.BasicProfile1_1)]
public   class  WebService : System.Web.Services.WebService  {

    
public WebService () {

        
//Uncomment the following line if using designed components 
        
//InitializeComponent(); 
    }


    [WebMethod]
    
public string HelloWorld() {
        
return "Hello World";
    }

    [WebMethod]
    
public string SayHelloTo(string Name)
    
{
        
return "Hello " + Name;
    }

      
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值