Tuesday, September 8, 2009

Calling Web services Through JavaScript/html

//this is your web method
[WebMethod]
public string HelloWorld() {
return "Hello World";
}


//Now use this html code to call web method use head tag for javascript



function InitializeService()
{

Myservice.useService("http://localhost:4765/WebServices/Service.asmx?wsdl", "MyMethod");
//you can use anything in the place of MyMethod
}
function tst(){
//alert('hi');
Myservice. MyMethod.callService("HelloWorld");
//if your web service contains some parameters then set parameters after function name like //("HelloWorld","1","admin")
}
functionShowResult(){
alert(event.result.value);
//Myservice.innerHTML= "Resultado : " + event.result.value;
}


// then on body onload write code
onload="InitializeService()" onresult="ShowResult()"

form id="form1" method="post"
// call javascript function on button click

button onclick="tst()" id="Button1">Call Add Web Method
//use htc file
div id="Myservice" style="behavior:url(webservice.htc)"



Note:
Friends Calling Web Services is a biggest problem with as , I found the logic behind it and I want to share.
First of all we should have a HTC file (U can download from
http://sites.google.com/site/santo01org/javascript-and-web-service
) which is nothing just a HTML file with the complete information of XML http and javascript( a common file for all)Then use the above JavaScript in call page and see the magic.

Best of Luck

No comments:

Post a Comment