Consuming an Asp.NET web service using SOAP protocol from JavaScript

There are many ways we can consume a web service
The three common protocols for accessing a .NET web service are
HTTP GET, HTTP POST and SOAP.
The trasport protocol used for them is HTTP. However a web sercie can work on any other internet protocol like SMTP, FTP, Jabber, and TCP.

For HTTP GET and HTTP POST method you can refer to these posts
https://nishantrana.wordpress.com/2007/10/22/calling-aspnet-web-service-from-javascript-ajax-passing-parameter/
https://nishantrana.wordpress.com/2007/10/22/calling-aspnet-web-service-from-javascript-ajax-passing-parameter-post/
The proxy class generated by Visual studio for us using the wsdl itself uses the SOAP.
Let’s take an example of calling the simple web service using SOAP through java script.
Say this is our sample web service

[WebService(Namespace = “http://myNamespace/”)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld(String name) {
return “Hello “+name;
}

}

The javascript code that we can use to call this web service using SOAP request would be this
<script type=”text/javascript”>
var xmlHttp;
function SoapCall()
{
// creatng the xmlHttp object
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
// Calling the web service using post and true means asynchronous call
xmlHttp.open(“post”, “http://localhost/WebSite/WebSite4/Service.asmx&#8221;, true);
// Setting the request header to let the web service identify the soap request we would be sending
xmlHttp.setRequestHeader(“Content-Type”,”text/xml; charset=utf-8″);
// http://myNamespace/HelloWorld – name of the webmethod
//[WebService(Namespace = “http://myNamespace/&#8221;)] which we had applied to our web service class
xmlHttp.setRequestHeader(“SOAPAction”,”http://myNamespace/HelloWorld&#8221;);
xmlHttp.onreadystatechange=doUpdate;
// setting the soap request body
var soapRequest=”<?xml version=’1.0′ encoding=’utf-8′?>” +
“<soap:Envelope xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance'”+
” xmlns:xsd=’http://www.w3.org/2001/XMLSchema&#8217; xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/’>”+
“<soap:Body>”+
“<HelloWorld xmlns=’http://myNamespace/’>”+
“<name>Nishant Rana</name> “+
“</HelloWorld>”+
” </soap:Body>”+
“</soap:Envelope>”;


xmlHttp.send(soapRequest);
return false;


}

function doUpdate()
{
debugger;
if(xmlHttp.readyState==4)
{
var responseXMLResult=xmlHttp.responseXML;
var result = responseXMLResult.lastChild.nodeTypedValue;
alert(result);
}
}
</script>

We will receive the following Soap Response
<?xml version=”1.0″ encoding=”utf-8″?>
<soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns:xsd=”http://www.w3.org/2001/XMLSchema”&gt;
<soap:Body>
<HelloWorldResponse xmlns=”http://myNamespace/”&gt;
<HelloWorldResult>Hello Nishant Rana</HelloWorldResult>
</HelloWorldResponse>
</soap:Body>
</soap:Envelope>

We can create our SOAP request by making use of the test page of our Web Service which we can open in IE.

Or else we can use Fiddler tool. (http://www.fiddlertool.com/fiddler/)

Bye..


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

10 thoughts on “Consuming an Asp.NET web service using SOAP protocol from JavaScript”

  1. You missed the point, Fernando. There are times that you want to consume a webservice strictly through javascript, and not dependent on any platform like .net or java.

    Like

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s