Calling Web Services from HTML Pages using JavaScript in Visual Studio 2010

Steps

  • Go to New Project
  • Select Language Visual C#
  • Select Web
  • Select ASP .NET Empty Web Application
  • Give a proper Name to your Web App like WSFromHTMLusingJS

Now Add a HTML File to the Project.

For this, Right-click on the Project, choose Add a New Item, and choose the HTML template.

Name this like  AddNumbersForm.html

This is a simple HTML Form taking three Numbers as input parameters

<!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>
    <title>Add Three Nos</title>

    <script type="text/javascript">

        function Init() {
            service.useService("WebService1.asmx?wsdl", "MyMath");      
        }

        var num1, num2, num3;

        function AddNos() {
            num1 = document.additionForm.num1.value;
            num2 = document.additionForm.num2.value;
            num3 = document.additionForm.num3.value;
            iCallID = service.MyMath.callService("AddThreeNos", num1, num2, num3);
        }

        function ShowResult() {
            alert(event.result.value);
        }
    </script>
</head>



 <body onload="Init()" id="service" style="behavior:url(webservice.htc)" onresult="ShowResult()">


<form name='additionForm'>

<table  cellpadding='10' align='center'>

<tr>
<td colspan="2" align="center">Add Three Numbers</td>
</tr>

<tr>
<td>Number 1: </td>

<td><input type="text" name ="num1" size="10" maxlength ="5" /> </td>

</tr>

<tr>
<td>Number 2: </td>

<td><input type="text" name ="num2" size="10" maxlength ="5" /></td>

</tr>

<tr>
<td>Number 3: </td>

<td><input type="text" name ="num3" size="10" maxlength ="5" /></td>

</tr>


<tr> <td colspan="2" align="center"><br/><br/>

<button onclick="AddNos()">Add</button>

<input type="reset" value ="Reset" />

</td></tr>

</table><br/><br/>


</form>

</body>
</html>

Now Add a Web Service to the Project

For this, Right-click on the Project, choose Add a New Item, and choose the Web Service template.

Name this WebServiceTest.asmx

When you create the web service by default you get a web method called Hello World.

Put this code below that and run your web service

[WebMethod]
        public int AddThreeNos(int num1, int num2, int num3)
        {
            return num1 + num2 + num3;
        }

Here to keep things simple we have created a very simple Web Service that returns the sum of three integer variables.

For calling webservices in HTML using JavaScript you need another file called webservice.htc
You can download this file from here.

webservice.htc

Web Service Behavior can be used to call remote methods from Web Services. The WebService behavior is implemented with an HTML Component (HTC) file as an attached behavior, so it can be used in Microsoft Internet Explorer 5 and later versions.

Download this file and put it in your Project Directory.

Run the project. You are done now. Congrats for Calling a WebService from an HTML Page using JavaScript.

Snapshot of the o/p

Addition No Form

Result from WebService

P.S-Try this only in Internet Explorer. You may not get the desired results in other browsers.

Agile Software Development Tool-Agilefant

Agilefant is an open source tool for managing agile software development. It brings together the perspectives of daily work, long-term product and release .

It has become quite popular in the industry where  Agile Methodology is used for Software Development.

Some of its features are:

  • Backlog Management
  • Product Management
  • Iterations
  • Dailywork
  • Timesheets

The UI is easy to understand and even a newcomer becomes comfortable using it.

Visit the website for more info and downloads.

http://www.agilefant.org/

A Search Query for your Loved one

I was just going through some random Status updates on Facebook and I got one amazing one.

It’s a Math Equation which when you do a Google Search renders a Heart.

Here’s the Equation

sqrt(cos(x))*cos(300x)+sqrt(abs(x))-0.7)*(4-x*x)^0.01, sqrt(6-x^2), -sqrt(6-x^2) from -4.5 to 4.5
Here’s another one, this one renders a heart with an arrow
sqrt(cos(2x))*cos(300x)+sqrt(abs(x))-0.7)*(4-x*x)^0.01 from -4.5 to 4.5,x from -3 to 3

These equations only work in Google if you use other Search Engines like Bing or Yahoo, it won’t work there. It’s because when you put any Math equation in Google, it will try to plot of a graph of that equation just like your scientific calculation.

I am attaching  screenshots for those who couldn’t get the correct result due to some problem. (Just now one of my friend couldn’t get the o/p) ;)

Amazing isn’t it. A different way to express your Geeky Love this Valentine. :D

Agneepath in a Software Engineer Style

Code ho bade bade…
Salon se phate pade…
Ek bhi LEAVE tu….
maang mat maang mat maang mat…
Agneepath Agneepath Agneepath…

Tu naa hasega kabhi…
Tu na khush rahega kabhi…
har Saturday office ane ki…
kar shapath kar shapath kar shapath…
Agneepath Agneepath Agneepath…

Ye mahan Project hai…
har koi REJECT hai…
CLIENT,BUG,REQUEST se…
lathpath lathpath lathpath…
Agneepath Agneepath Agneepath…

Interesting Isn’t is…Dedicated to all the Software Engineers from India who loves Hindi Movies :D