Calling Web Services From Html Pages using Javascript

Introduction

Hello everyone. In this article i wanted to show you, 'how to call a web service and pass parameters to its methods through html pages'. I hope it will be useful.

For this article we need to create a web service and a web site. Assume that our web service includes these methods...
[WebMethod]
public string HelloWorld() {
    return "Hello World";
}


//This method takes your birth year, month and day,
//calculates your age and return the year.
[WebMethod]
public string GetAge(int year, int month, int day) {
    DateTime birthDate = new DateTime(year, month, day);
    long age = new DateTime(DateTime.Now.Ticks - birthDate.Ticks).Year - 1;
    return "You are " + age.ToString() + " years old.";
}


//This method caches the datetime for 4 seconds.
//also a simple cache implementation.
private const int CacheTime = 4; // seconds
[WebMethod(CacheDuration = CacheTime,
Description = "As simple as it gets - the ubiquitous Get Date Time.")]
public string GetDateTime() {
    return DateTime.Now.ToString();
}


Html pages that call this web service


Your web page directory should include the 'webservice.htc' file. This file adds a behavior to html files for calling web services. You can find this file in the *.zip file you will download. I create 3 web pages for this solution;
Note : please don't forget to change port number for web service access in your html pages
   1. HelloWorld.htm (calls Hello World Method)
 <html>
  <head>
   <title>Hello World</title>
    <script language="JavaScript">
     var iCallID;
     function InitializeService(){
      service.useService("http://localhost:1394/MyWebService.asmx?wsdl", "HelloWorldService");
      service.HelloWorldService.callService("HelloWorld");
     }
     function ShowResult(){
      alert(event.result.value);
     }
    </script>
   </head>
  <body onload="InitializeService()" id="service" style="behavior:url(webservice.htc)" onresult="ShowResult()"> </body>
 </html>
   2. GetAge.htm (calls GetAge method, takes 3 parameters)
 <html>
  <head>
   <title>UseSwap</title>
    <script language="JavaScript">
     function InitializeService(){
      service.useService("http://localhost:1394/MyWebService.asmx?wsdl", "GetAgeService");
     }
     var StrYear, StrMonth, StrDay;
     function GetAge(){
      StrYear = document.DemoForm.StringYear.value;
      StrMonth = document.DemoForm.StringMonth.value;
      StrDay = document.DemoForm.StringDay.value;
      service.GetAgeService.callService("GetAge", StrYear, StrMonth, StrDay);
     }
     function ShowResult(){
    alert(event.result.value);
      }
     </script>
    </head>
     <body onload="InitializeService()" id="service" style="behavior:url(webservice.htc)" onresult="ShowResult()">
      <form name="DemoForm">
       Year : <input type="text" name="StringYear"/>
       Month : <input type="text" name="StringMonth"/>
       Day : <input type="text" name="StringDay"/>
       <button onclick="GetAge()">Get Age</button>
      </form>
     </body>
 </html>
   3. GetDateTime.htm (returns cached value)
 <html>
  <head>
   <meta http-equiv="refresh" content="2" />
   <title>Get Date Time</title>
   <script language="JavaScript">
    var iCallID;
    function InitializeService(){
     service.useService("http://localhost:1394/MyWebService.asmx?wsdl", "GetDateTimeService");
     service.GetDateTimeService.callService("GetDateTime");
    }
    function ShowResult(){
     alert(event.result.value);
    }
   </script>
  </head>
  <body onload="InitializeService()" id="service" style="behavior:url(webservice.htc)" onresult="ShowResult()">
  </body>
 </html>


As listed above there are some javascript code written. But so easy to understand. First we add html page a behavior to access web service, then we make initialization and call the web service metod. Soon we get the result by event.result.value. That's all.
You can just copy and paste this code into your html pages and it works. Try the source code if you want. I hope it will be useful. Bye for now;)  <script src="/script/togglePre.js" type="text/javascript"></script>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值