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

Please 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

CRM for You and Me

Customer Relationship Mgt App for the Professional

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

Two Bite Tips

Valuable Tips To Grow Your Business

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM

EVOLVED365

Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

Sara Lagerquist

No Code Customization Concepts

innovativeaj

developer, designer, writer, fun loving, patriotic, humble and a sweet person inside out :) love and respect people who have a great sense of humor.

Temmy Wahyu Raharjo

Dreaming to be a clean coder and TDD minded programmer.

Transform 365

We blog about problems we face and code we write to help others

%d bloggers like this: