Varad, The .NET Guy!

Exploring the excitement of Microsoft .NET and much more..

Calling WebServices using Javascript

If you are using Microsoft IE 5 or later, you can use the behavior/HTML-Component "WebService" to access a Web service. The "WebService" behavior communicates with Web services over HTTP using Simple Object Access Protocol (SOAP).

To use the "WebService" behavior, you must attach it to an element using the STYLE attribute, as follows:

<DIV ID="GiveItAName"
STYLE="behavior:url(webservice.htc)"></DIV>

A complete example taken from the Microsoft Web site is as follows:

<html>
<head>
<script language="JavaScript">
var iCallID;

function init()
{
service.useService
("
http://myserver.com/services/myservice.asmx?WSDL",
                   "servicename");
}

function onmyresult()
{
   if ((event.result.error)&&(iCallID==event.result.id))
   {
      var xfaultcode = event.result.errorDetail.code;
      var xfaultstring = event.result.errorDetail.string;
      var xfaultsoap = event.result.errorDetail.raw;

      // Add code to output error information here
      alert("Error ");
   }
   else
   {
      service.innerHTML= "The method returned the result: "
                         + event.result.value;
   }
}
</script>
</HEAD>
<body onload="init();">
<BR>
Enter a Value <input type='text' id='param1'>
<BR>
<button onclick='iCallID = service.servicename.callService
("ProcedureName", param1.value);'>Call A Web Method</button>
<div id="service"
     style="behavior:url(webservice.htc)"
     onresult="onmyresult();">
</div>
</body>
</html>

 

Comments

vk said:

Hi,

I get an security error when I try using webservices using javascript. Is there a work around it?

thanks

# June 23, 2004 6:02 PM

Varad said:

Where exactly are you getting the error? That would help me in providing some solution.
# June 24, 2004 8:29 AM

vk said:

Windows Error:
"This page is accessing information that is not under its control. This poses a security risk. Do you want to continue?"
# June 24, 2004 9:21 AM

Varad said:

It looks like the error is due the security settings of your browser. Nothing to do with the code.

On your Internet Options, goto Advanced Tab and check under Security any items regarding warning is checked. If so uncheck that and try running the page. Since the code is using HTML Component and calling webservice as http request, you might face the problem if you have restriction on your browser settings.

Please check the settings and let me know.
# June 24, 2004 11:53 AM

vk said:

Thanks Varad. You were right about the browser security settings. Also, is there a work-around to make this work with Netscape 6 and above. Thanks in advance.

# July 5, 2004 2:50 PM

Varad said:

Right now, I am not sure..Will let you know soon..
# July 5, 2004 3:33 PM

Srinivas said:

Getting this script error...."Object can't support this property" while trying to use this service.useService(

Any idea?
Advance Thanks
Srinivas
# July 9, 2004 4:28 AM

Varad said:

How you are calling the service. Could you please post the entire line of code?
That would help to provide a solution.
# July 9, 2004 4:40 PM

Srinivas said:

Pls have the code...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">


<script language="JavaScript">
var iCallID;

function init()
{
alert("here ");
service.useService("http://myserver.com/services/myservice.asmx?WSDL", "servicename);
}

function onmyresult()
{
if ((event.result.error)&&(iCallID==event.result.id))
{
var xfaultcode = event.result.errorDetail.code;
var xfaultstring = event.result.errorDetail.string;
var xfaultsoap = event.result.errorDetail.raw;

// Add code to output error information here
alert("Error ");
}
else
{
service.innerHTML= "The method returned the result: "
+ event.result.value;
}
}
</script>
</HEAD>
<body onload="init();">

<BR>
Enter a Value <input type='text' id='param1'>
<BR>
<button onclick='iCallID = service.servicename.callService
("ProcedureName", param1.value);'>Call A Web Method</button>
<div id="service" style="behavior:url(webservice.htc)" onresult="onmyresult();">
</div>


</BODY>
</HTML>
# July 12, 2004 9:05 AM

Varad said:

Is the the correct syntax you specified?

service.useService("http://myserver.com/services/myservice.asmx?WSDL", "servicename);

end quote " is missing for servicename parameter.
Please check and revert back.
# July 12, 2004 3:33 PM

Srinivas said:

I had used this.Even this is not working
service.useService("http://pips7.corporate.ge.com/PageWebService/PageService.asmx?WSDL","servicename");
# July 13, 2004 7:50 AM

Varad said:

Do you have the WebService behavior file on the same folder of this HTML or asp file?

Both the .htc file and the Web Service must be on the same Internet domain as the HTML file that uses them.

Please refer the MSDN article for more information..

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndude/html/dude01222001.asp
# July 13, 2004 1:49 PM

TrackBack said:

DoNews.net
# August 8, 2004 12:20 PM

huqingyu said:

It have memory leak?
you can test :
for(i=1;i<100000;i++)
{
service.servicename.usecall("method");
}
# August 10, 2004 2:16 AM