Category: Web Services

08/11/06

Permalink 08:16:32 pm, Categories: Microsoft.NET, Web Services, 255 words   English (US)

How to call a web service using Javascript?

For example:
You want to get a client name from the client list. "GetClientName" function exposed through web service name called "Service1.asmx" and the function takes "Client Code" as a passing variable.

Copy the WebService behavior (webservice.htc file) into the Web project folder.

You can download webservice.htc file at this link: http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/samples/internet/behaviors/library/webservice/default.asp

Copy this javscript code and name it webservice.js and copy into the Web project folder.

var iCallID1;

function onBlur()
{
Get_ClientName();
}

function onEnter()
{
var kCode=event.keyCode;
if(kCode==13)
{
Get_ClientName();
}
}

function onWSresult()
{
if (event.result.error)
{
var xfaultcode = event.result.errorDetail.code;
var xfaultstring = event.result.errorDetail.string;
var xfaultsoap = event.result.errorDetail.raw;

alert("Error: " + xfaultcode + "|" + xfaultstring + "|" + xfaultsoap + "|");
}
else
{

if (iCallID1==event.result.id) {
document.all("ClientName").innerHTML = event.result.value;
}
}
}

function Get_ClientName() {
if (document.all("ClientCode").value != "") {
service.useService("http://www.charora.com/WebServices/Service1.asmx?WSDL","MyWebService");
iCallID1 = service.MyWebService.callService("GetClientName", document.all("ClientCode").value);
}
else {
alert ("Please enter client code ... ");
}
}

Create test.htm and include webservice.js in the test.htm

add a DIV tag and with following attributes

id="service"
style="behavior:url(webservice.htc)"
onresult="onWSresult();"

add input tag and with following attributes

type="text"
name="ClientCode"
Text=""
onblur="onBlur();"
onkeypress="onEnter();"

add another input tag and with following attributes

type="text"
name="ClientName"
Text=""

Finally, browse the test.htm file and enter client code and hit enter.

You will get the client name.

Blog - All

This blog is to publish all articles.

This allows you to keep track of all the technical posted on this system.

September 2010
Mon Tue Wed Thu Fri Sat Sun
<<  <   >  >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      

Search

Archives

Misc

Syndicate this blog XML

What is RSS?

powered by
b2evolution