Wednesday, August 09, 2006

Calling WebService from Javascript



This is webservice call from Javascript..
------------------------------------------
Webservice Name : Calculation.asmx

[WebMethod]
public int Add(int a,int b)
{
return a+b;
}
-------------------------------------------
// SOAP Structure.

string request =

"<soap:Envelope xmlns:xsi='http://wwww.w3.org/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/XMLSchema'
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
<soap:Body>
<Add> // This is function name
<a> 1 </a> // Parameter 1
<b> 2 </b> // Parameter 2
</Add>
</soap:Body>
</soap:Envelope>";


// Create object for XML HTTP.
var objhttp = new ActiveXObject("MSXML2.XMLHTTP");
//Mention where the webservice is running [URL]
objhttp.open("POST","http://google.com/Calculation.asmx);
// function name
objhttp.setRequestHeader("SOAPAction","http://tempuri.org/Add");
objhttp.setRequestHeader("Content-Type", "text/xml");
objhttp.send(request);

//Result in the form of xml like this <Result> 3 </Result>.you should have load this xml string into DOM then Parse as per your req.
var xml = objhttp.ResponseText;
--------------------------------------------------------------------------------------



No comments: