Click here to Skip to main content
15,171,993 members
Articles / Programming Languages / C#
Posted 29 Jun 2006


63 bookmarked

Calling Web Services from HTML Pages using JavaScript

Rate me:
Please Sign up or sign in to vote.
3.10/5 (29 votes)
29 Jun 2006CPOL1 min read
This article will help you to call a web service directly from an HTML page
Sample Image - CallWebServiceFromHtml.jpg


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...

public string HelloWorld() {
    return "Hello World";

//This method takes your birth year, month and day, 
//calculates your age and return the year. 
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 '' 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):
      <title>Hello World</title>
       <script language="JavaScript">
        var iCallID;
        function InitializeService(){
        function ShowResult(){
     <body onload="InitializeService()" id="service"
       style="behavior:url(" onresult="ShowResult()"> </body>
  2. GetAge.htm (calls GetAge method, takes 3 parameters):
       <script language="JavaScript">
        function InitializeService(){
        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(){
        <body onload="InitializeService()" id="service"
       style="behavior:url(" 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>
  3. GetDateTime.htm (returns cached value):
      <meta http-equiv="refresh" content="2" />
      <title>Get Date Time</title>
      <script language="JavaScript">
       var iCallID;
       function InitializeService(){
       function ShowResult(){
     <body onload="InitializeService()" id="service"
       style="behavior:url(" onresult="ShowResult()">

As listed above, there is some JavaScript code written. But it is so easy to understand. First, we add HTML page behavior to access web service, then we make initialization and call the web service method. 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 should work. Try the source code if you want. I hope it will be useful. Bye for now.


  • 29th June, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gökmen BULUT
Web Developer
Turkey Turkey
Borned in İstanbul/TURKEY....
Loves İstanbul...

Comments and Discussions

QuestionNot working Pin
Member 126701394-Sep-18 3:50
MemberMember 126701394-Sep-18 3:50 
QuestionObject doesn't support property or method 'useService' Pin
Kalpesh Shah16-May-18 1:55
MemberKalpesh Shah16-May-18 1:55 
GeneralMy vote of 1 Pin
Lars Bernstorff Hansen2-Oct-14 6:22
MemberLars Bernstorff Hansen2-Oct-14 6:22 
GeneralMessage Closed Pin
18-Sep-15 22:36
MemberMohammad Tuaha18-Sep-15 22:36 
GeneralRe: My vote of 1 Pin
Kalaivanan.E12-Feb-19 20:14
MemberKalaivanan.E12-Feb-19 20:14 
QuestionThis will work - Do the changes as mentioned below Pin
Abishek Aradhya26-Sep-13 22:22
MemberAbishek Aradhya26-Sep-13 22:22 
Questionnot working Pin
MM GEHLOT20-Jul-13 3:39
MemberMM GEHLOT20-Jul-13 3:39 
QuestionIt doesn't work Pin
AVITALbe21-Apr-13 5:38
MemberAVITALbe21-Apr-13 5:38 
AnswerRe: It doesn't work Pin
praveendubey177-Apr-14 6:03
Memberpraveendubey177-Apr-14 6:03 
QuestionRe: It doesn't work Pin
Member 118179859-Jul-15 0:56
MemberMember 118179859-Jul-15 0:56 
GeneralMy vote of 5 Pin
crcklssp7-Mar-13 20:25
Membercrcklssp7-Mar-13 20:25 
QuestionNot working Pin
tej narayan maurya7-Jan-13 19:40
Membertej narayan maurya7-Jan-13 19:40 
QuestionI don't know why this doesn't work :( Pin
hipatuwa27-Oct-12 20:45
Memberhipatuwa27-Oct-12 20:45 
QuestionCrossBrowser Issue Pin
Member 816917621-Aug-12 2:12
MemberMember 816917621-Aug-12 2:12 
GeneralMy vote of 3 Pin
Yogesh Potdar8-Jul-12 8:44
MemberYogesh Potdar8-Jul-12 8:44 example works only in IE Pin
Shamims8-Jun-12 8:44
MemberShamims8-Jun-12 8:44 
QuestionDataSet and DataTAble Pin
Denisrq22-Mar-12 11:07
MemberDenisrq22-Mar-12 11:07 
AnswerRe: DataSet and DataTAble Pin
Yogesh Potdar8-Jul-12 8:48
MemberYogesh Potdar8-Jul-12 8:48 
GeneralRe: DataSet and DataTAble Pin
Denisrq9-Jul-12 6:46
MemberDenisrq9-Jul-12 6:46 
Questioncorss domain? Pin
JasonJoh15-Mar-12 6:53
MemberJasonJoh15-Mar-12 6:53 
GeneralMy vote of 5 Pin
muhammadali7-Mar-12 21:04
Membermuhammadali7-Mar-12 21:04 
Newserror Pin
ranadheerk28-Feb-12 23:43
Memberranadheerk28-Feb-12 23:43 
QuestionHow come... Pin
pergh23-Mar-11 9:37
Memberpergh23-Mar-11 9:37 
GeneralMy vote of 5 Pin
ishmayel14-Nov-10 23:59
Memberishmayel14-Nov-10 23:59 
GeneralMy vote of 2 Pin
NormDroid27-Oct-09 22:53
professionalNormDroid27-Oct-09 22:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.