Consuming a Web Service from an HTML Page
page 4 of 5
by Rajesh Toleti
Feedback
Average Rating: 
Views (Total / Last 10 Days): 58884/ 431

Explanation

We call the getWSDL JavaScript function on the body load event, as shown below.

Listing 2: getWSDL Function

<body onload="getWSDL()">

This function gets the WSDL document from the Web service. In this function we call the useService method of the myWebService service. We pass two parameters to this method.  The first parameter is the URL of the Web service appended by "?WSDL."  The second parameter is the friendly name of the Web service; you can use whatever name you like.

Listing 3: myWebService Example

myWebService.useService(
    "http://www.taryatechnologies.com/ws/IP2countryws.asmx?WSDL",
    "IP2Country");

When you view the style attribute of the div tag, you can see a bit of weird code. This is the heart of the HTML page from which we are deriving Web service consumption capabilities. I will explain the getResult function later.

Listing 4: Div Tag Style Attribute

<div id="myWebService" 
style="behavior:url(webservice.htc)"
onresult="getResult()"></div>

In the HTML page, we are collecting an IP address as input. If you provide an invalid IP address you will get an error message. An IP address may not be your machine’s IP address if you are on a LAN; in that case, it will be the WAN IP of your internet gateway (e.g., a wireless router).

When you press the "getCountry" button, it calls the Lookup function via an onclick handler.

Listing 5: getCountry() Button Event

myWebService.IP2Country.callService("getCountry", txtIP.value);

We now pass the method name of the Web service as the first parameter and the value of the IP address provided as second parameter. Once the result is sent back to the HTML page, it raises the result event, and then the getResult function is called. The getResult function was specified as the result event handler by the onresult parameter of the div tag shown in Listing 4.

Listing 6: getResult() Function

country.innerText = event.result.value;

This function assigns the result to the "country" span element.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 3 and 4 and type the answer here:

User Comments

Title: with reference to MATTO CASATI   
Name: Zaf Khan
Date: 11/27/2008 7:52:51 PM
Comment:
THanks Matto, :)

I used this link to transalate your page to ENGLISH....
ALso......
Its the Best Translation of PROPER English yet!!!

http://babelfish.yahoo.com/translate_url?doit=done&tt=url&intl=1&fr=bf-home&trurl=http%3A%2F%2Fwww.guru4.net%2Farticoli%2Fasp-soap-client%2F&lp=it_en&btnTrUrl=Translate
Title: Malakas   
Name: Paparas
Date: 10/6/2007 6:43:03 AM
Comment:
To mouni to lene Giwta kai ton poutso Panagiwta!!!
Title: undefined error   
Name: Sheikh Abdul Jaweed
Date: 8/31/2007 11:35:26 AM
Comment:
get the error as "undefined"
Title: "undefined" result   
Name: nis mo
Date: 8/19/2007 6:12:40 PM
Comment:
i m trying to execute it and getting "undefined" result..
Same happening with my webservice too..what could be the problem?
my email is nissmor@gmail.com
Title: does this support firefox browser   
Name: kings
Date: 8/18/2007 3:49:58 AM
Comment:
hey its ok in internet explorer but it does not work in fire fox ., how to invoke this operaitons in firefox
Title: Working IP address to get the country name   
Name: Harry
Date: 7/11/2007 9:10:42 AM
Comment:
I tried with manu ipaddresses. can you please tell me with what ip address can i test this service
Title: Good   
Name: Mohammed Husamuddin
Date: 6/18/2007 2:00:31 AM
Comment:
Hava this error, Line 19
Error : 'myWebService.IP2Country' is null or not an object
A Runtime Error has occured
Title: return value wrong   
Name: yogesh
Date: 4/21/2007 7:36:24 AM
Comment:
Hey thanks for posting.
but i m trying to execute it and getting "undefined" result..
Same happening with my webservice too..what could be the problem?
Title: does not work on FireFox   
Name: Jah Hasangjekaj
Date: 3/10/2007 4:21:53 PM
Comment:
How can I make this work in Firefox
Title: oh no...   
Name: fudi
Date: 2/19/2007 9:36:30 AM
Comment:
i don't like such articles. it starts promising, but actually tells just half of the story. people who alreay know about web services and consuming them will understand anyway, all others are lost. why not provide the complete source code (also the web service)? i'm sure you'd know how to explain, so why don't you?
Title: Otto   
Name: Shiney Tracky Dacks
Date: 10/5/2006 1:28:13 AM
Comment:
Anthony Kasses what are you talking about ???
Title: Great Explanation!   
Name: DP
Date: 7/16/2006 5:19:22 PM
Comment:
Well, I have seen a dozen or more articles on how to use web services. This is by far the most basic explanation of how to get a web service to work. From here you can move t o WSDL, SOAP, UDDI etc., but this was great (easier than w3schools). Oh, as for not working in other browsers, I believe the behavior attribute is only recognized by MSIE, so the problem is not your code, but the other browsers.
Title: Consuming Web Service in ASP.NET   
Name: LeProgrammeur
Date: 6/11/2006 9:13:02 PM
Comment:
Hi!

I posted a tutorial showing the basic steps to consume a web service in asp.net at the link www.KYNOU.com.
Go to the link above and search for: Consuming a Web Service
There is also a chat room in this same link where I try to spend as more time as I can to answer questions.
:)
Title: Complex Return Values via HTC   
Name: Dan Fitzgerald
Date: 3/6/2006 5:24:02 PM
Comment:
Great article!

I was wondering about the return values. You mentioned that only simple return values can be returned. So, if I am interfacing with an existing web service that returns say a struct such as:

struct Return
{
int ID;
string the_string;
}

Is there a way to parse through this as a return value at all or do I need to ask the web service provider to modify to return a comma-separated string for example?

Thank you,

Dan
Title: Cross Domain Calls   
Name: Ryan Smith
Date: 12/19/2005 10:27:01 AM
Comment:
That was a great article. Is there anyway to make a webservice all across different domains? Or is the only way to accomplish this to handle it on the server site?
Title: Great Article   
Name: Don Logan
Date: 12/6/2005 9:55:30 AM
Comment:
Great clear article on Web services.
Title: which protocol is this webservice uisng in communaication   
Name: Deepak
Date: 11/28/2005 6:16:58 AM
Comment:
Hi,
this is an excellent stuff..but has few drawback..if only run in IE..and i just want to knwo which protocol(GET or POST or SOAP with HTTP) is it using..bcoz GET and POST are not secure ....email me regarding this to deepak@arcadix.com
Title: Software Developer   
Name: Ram
Date: 11/19/2005 12:25:26 AM
Comment:
Really this posting is very informative, but it is not complete.The description abt the getresult function and useservice is not complete..can u give me the complete details.

My mail-ID is : dajji@sify.com
Title: Very well explained   
Name: Anthony Kasses
Date: 11/14/2005 7:37:44 PM
Comment:
Great article, ...however I'm getting
"This page is accessing information that is not under it's control. This poses a security risk. Do you want to continue?" (yes/No).

I'm using Win 2003 and IE 6.

Any thought?
Title: Goood Work   
Name: Steve Rock
Date: 11/14/2005 5:34:00 AM
Comment:
Nice Work
Title: mr   
Name: alok
Date: 10/25/2005 8:53:46 AM
Comment:
good stuff!!!
Title: Test with error   
Name: came
Date: 10/21/2005 3:05:03 AM
Comment:
Hava this error, Line 19
Error : 'myWebService.IP2Country' is null or not an object
A Runtime Error has occured
Do you wish to Debug?
Title: Old technology..   
Name: Carlos
Date: 10/20/2005 11:32:18 AM
Comment:
Try AJAX, XmlHttp or something else.. because your solution has too much limitations for serius web develpment..
Title: Another way (cross browser!)   
Name: Matto Casati
Date: 10/20/2005 6:02:05 AM
Comment:
In my article (http://www.guru4.net/articoli/asp-soap-client/) you can find another way to consuming a Web Service with AJAX. My solution use pure javascript + XMLHttp and it's cross-browser (tested on IE, Moz, FireFox and Safari). The article is only available in italian language, sorry (but... code is code!)
Title: SSE   
Name: Neeraj Badaya
Date: 10/19/2005 10:17:19 PM
Comment:
Hi,

This is now obsolete now you have much better option of XMLHTTP which will work fine from the Javascript and need not to be include webservice.htc also.

Yes, it will work only for IF-5.0 and greater.
Title: Only in IE   
Name: lordm
Date: 10/11/2005 12:05:39 PM
Comment:
Yeah, but this only works in IE. What about FF? Maybe use XMLHTTP, AJAX or something..
Title: create a proxy web service   
Name: Bit Banger
Date: 10/10/2005 2:48:48 PM
Comment:
I _love_ this article. I can use it to excellent effect in many places in my application.

Anyone care to post the code for creating a proxy for "http://www.taryatechnologies.com/ws/IP2countryws.asmx"?
Title: Web services using javascript   
Name: K.Senthil Kumar (Sensoft2000)
Date: 10/7/2005 11:18:07 PM
Comment:
Wow.. Its really new information to me...
Title: programer   
Name: s k yadav
Date: 10/7/2005 4:17:07 PM
Comment:
I find this artical intresting. It is good..........hope you too
Title: Returning Complex Types   
Name: Mark Hildreth
Date: 10/7/2005 3:17:52 PM
Comment:
It is possible to return complex types from the webservice behavior, just make the webservice return type a struct with public properties. You can also return arrays or expose public properties that are arrays within a struct.
Title: Major Limitation   
Name: Ravi K
Date: 9/16/2005 3:47:07 PM
Comment:
Good article, but this only works with Internet Explorer, which I think is a major limitation.
Title: Technical Associate   
Name: Pavan Kumar C
Date: 8/31/2005 6:39:25 AM
Comment:
Great post that gives a clear understanding about the concept.

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 12/4/2008 3:25:07 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search