Consuming CurrencyConverter web service through JavaScript


For getting the currency conversion rate we could use the following web service provided by Generic Objects Technologies Ltd.

Check out the web services provided by them

To consume their currency converter web service we could use the following JavaScript code

var xmlHttp;
function SoapCall() {
// creatng the xmlHttp object
xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
// Calling the web service using post and true means asynchronous call“post”, “”, false);
// Setting the request header to let the web service identify the soap request we would be sending

xmlHttp.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);

xmlHttp.setRequestHeader(“SOAPAction”, “http://www.webserviceX.NET/ConversionRate”);

var soapRequest = “<?xml version=’1.0′ encoding=’utf-8′?> ” +
“<soap12:Envelope xmlns:xsi=’; xmlns:xsd=’; xmlns:soap12=’’>&#8221; +
” <soap12:Body>” +
”   <ConversionRate xmlns=’http://www.webserviceX.NET/’>&#8221; +
”     <FromCurrency>USD</FromCurrency>” +
”    <ToCurrency>INR</ToCurrency>” +
”  </ConversionRate>” +
“</soap12:Body>” +




Understanding and Using AJAX

Suppose this is our simple ASP.NET page i.e. MyPage.aspx which would be called using AJAX

Code for MyPage.aspx.cs

protected void Page_Load(object sender, EventArgs e)


// no data is passed

if (Request.QueryString[“myGetData”] == null)


Response.Write(“Hello World”);


// if GET method is used to call this page

else if (Request.QueryString[“myGetData”] != null)


Response.Write(“Hello “ + Request.QueryString[“name”].ToString());


// if POST method is used to call this page

else if (Request.Form[“myPostData”] != null)


Response.Write(“My Post Data is “ + Request.Form[“myPostData”].ToString());



Delete everything from MyPage.aspx page just keep the page directive otherwise html tags would also be sent as a part of response.

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Mypage.aspx.cs” Inherits=”MyPage” %>

Now the javascript code to call the page asynchronously using AJAX. Comment/Uncomment the particular section of code to test the functionality

<script type=”text/javascript”>

var request=null;

function CreateRequest()




// create a new object to talk HTTP with a web server.

// XMLHttpRequest – works on Safari,Firefox, Mozilla, Opera , IE 7 and most other browsers

request=new XMLHttpRequest();






// Msxml2.XMLHTTP – Most version of IE support this

request=new ActiveXObject(“Msxml2.XMLHTTP”);






//Microsoft.XMLHTTP – but for some, we’ll need this other type

request=new ActiveXObject(“Microsoft.XMLHTTP”);









alert(‘Error creating request object’);



// i.e. space with %20

// if no data is to be send

var url=“MyPage.aspx”;

// open-initialize the connection

// GET – specify how to send data to the server

// url- url to send the request

// asynchrounous – true“GET”,url,true);

// telling the browser what to do with the server’s response

// i.e. the function to be called when response is received

// onreadystatechange- this property affects every ready state not just the one indicating

// that the server is finished with request


// send the request

// no data is required so send null

// for using send() to pass data to a server requires POST method for the request and need to specify content type


// if data is to be send using GET i.e. as query string

var name=“MyName”;

// escape() function replaces characters like space with something that will work as a part of a request URL.

var url=“MyPage.aspx?myGetData=”+escape(name);“GET”,url,true);



// if more data is to be send than use post

var name=“MyName”;

var url=“MyPage.aspx”;“POST”,url,true);


//setRequestHeader()- allows us to add information to the request usual intended for use by the server

// Content-Type – is the name of the request header

// application/x-www-form-urlencoded – the value of the request header

// this tells the server that the data is encoded like it would be in a request URL

// for passing XML content replace “application/x-www-form-urlencoded” with “text\xml”


// specify the data to be send

// escape() function replaces characters like space with something that will work as a part of a request URL.




// updatepage – this function will get run every time the ready state changes

function updatepage()


// readyState=this property of the request object stores the current state

// if 4 means server is done with the request

// if 0 i.e uninitialized

// if 1 i.e open, the object has been created but the send method has not been called

// if 2 i.e. sent, the send method has been called.

// if 3 i.e. receiving , responseText is not available but some data has been received

// if 4 i.e. loaded, all the data has been received, responseText is available



// check for the status send by the server

// if 200 i.e. everything is fine

// if 404 i.e. if the page specified in the url is not found



var myResponse=request.responseText;





alert(“Error! Request status is “ +request.status);





<input id=”Button1″ type=”button” value=”button” onclick=”CreateRequest()”/>


Calling a webservice from an asp page

Say we have created a simple web service like this

public string Hello(String name) {
return “Hello “+name ;

Now we want to call the web service from inside our asp page,

for this we can make use of the following code

Dim myPostData
Dim name
name=”Mickey Mouse”
myPostData=”name=” & name
Dim xmlhttp
Dim postUrl
postUrl = “http://servername/MyService/Service.asmx/Hello&#8221;
Set xmlhttp = server.Createobject(“MSXML2.XMLHTTP”)
xmlhttp.Open “POST”,postUrl,false
xmlhttp.setRequestHeader “Content-Type”,”application/x-www-form-urlencoded”
xmlhttp.send strWebServideDetails
Response.Write(“<b>This is the message i recieved</b>” +xmlhttp.responseText)



Creating a simple Hello World ASP.NET AJAX web page.


Here we will create a simple ASP.Net Ajax webpage having a button and textbox control in it.
On the click of button we will fill the textbox with “Hello world” string returned from the server without refreshing our webpage.

1) Create a new ASP.NET Ajax web site(Visual Studio 2005)or ASP.NET web site (version 3.5) if we are using Orcas(Visual studio 2008)

2) Add a ScriptManager control in the page( if it isn’t there)

3) Add button and textbox server side control.

<asp:TextBox ID=”TextBox1″ runat=”server” />

<asp:Button ID=”Button1″ runat=”server” Text=”Button” />

4) Add this event Handler for button click event

protected void Button1_Click(object sender, EventArgs e)
TextBox1.Text=”Hello World”;

4) Now to make our page AJAX enabled the only thing we need to add is

<asp:UpdatePanel id=up1 runat=server>

<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>
<asp:Button ID=”Button1″ runat=”server” onclick=”Button1_Click” Text=”Button” />


Just wrap our server side controls inside update panel control’s content template.

5) That’s it. Now run the application and click on the button the textbox should get filled with Hello World string without any refresh of the page.

Right now what we saw was a server-centric approach using updatePanel control.

We can do the same thing using client-centric approach using ASP.NET AJAX client library. Check this post



Creating and Calling ASP.NET AJAX Web Service


First we will create a new ASP.NET Ajax web site(Visual Studio 2005)
or ASP.NET web site (version 3.5) if we are using Orcas(Visual studio 2008)

Through Add New Item, add a new web service WebService.asmx in the website.

1) Add this in webservice.cs

using System.Web.Script.Services;

2) Add a ScriptService attribute to our webservice class

[WebService(Namespace = “;)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

3) We’ll use the default Hello World web service. So just save the web service and view it in browser to test it.

Now change the url of the webservice in browser and append “/js” to it

Press enter it will ask you to save the file,
just save and open it in notepad

We will see something like this

var WebService=function() {
this._timeout = 0;
this._userContext = null;
this._succeeded = null;
this._failed = null;
_get_path:function() {

This is a javascript proxy class which the our client side ajax library will use to make call to our web service.

4) Add a script manager control to our default.aspx page ( if not already there), and add the reference to our web service

<asp:ScriptManager ID=”ScriptManager1″ runat=”server”>
<asp:ServiceReference Path=”WebService.asmx” />

5) Now add a simple html text control on the default.aspx page. We will be calling our web service and fill the text box value returned from the webservce (“Hello World”).

<input id=”Text1″ type=”text” />

6) Put this script on your default.aspx page

<script type=”text/javascript”>

function pageLoad()

function onSuccess(result)


7) Now just view the page in browser we should see the textbox filled with “Hello World” string returned from our web service.

8) Or if we want to call it in the click of the button. add a html button control.

<input id=”Button1″ type=”button” value=”button” />

9) Replace the above script with this one

<script type=”text/javascript”>
var myButton=null;
function pageLoad(sender,e)

function btn_click(sender,e)

function onSuccess(result)

In the above example we saw a client centric approach, the same thing can be done very easily by making use  of server centric approach in ASP.NET AJAX framework.