Wednesday 7 November 2012

ASP.net 1.1 web service call via Javascript

Working on legacy code is like visiting an Old Fort, its strong robust for the most part, but under maintained and maybe not practice  with current times but you just cannot wish it away, especially if there is money involved. I worked on something  that I had created in 2005 and have been working on it off and on but this was something different, removing post back from asp.net form. It would be a simple task for any project in ASP.net 2.0  and upwards but in ASP.net 1.1 it different, you don't have any native support for AJAX, so you need to use custom Java script to apply ajax. The second part of my problem was that there was lot of DB interactions so I needed to use a web service that was built in ASP.net Development 1.1 and to had no native support of AJAX or JS while I was successful calling "Hello world " method without parameters but sending parameters was not working while I digged around I could not find a suitable solution. I finally came to the solution of using the native MSXML2 activex object as native call method with passing SOAP envelop. 

We understand that it not a universal solution but with limitations we had and since we where aware of the fact the legacy solution was a IE only solution so we concluded it to be the best option available, we are open to any new solutions that anybody out there might have or come across, please do share with us.

Sample Code


var xmlObj = new ActiveXObject("Msxml2.DOMDocument") ;
// Creating Soap Envelope  as string    
var sXml  = "<?xml version=\"1.0\" ?>" ;
                sXml += "<soap:Envelope "
                sXml += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " ;
                sXml += "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " ;
                sXml += "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" ;
                sXml += "<soap:Body>" ;
                sXml += "<SubmitResult xmlns=\"http://tempuri.org/\">" ; // Method Reference
                sXml = sXml + "<ID>" + vID  + "</SRID>" ; // Parameter values being set vID contains a valid Id
                sXml = sXml + "<Result>" + vResult  + "</Result>" ; // vResult is valid result to be sent to database
                sXml += "</SubmitResult></soap:Body></soap:Envelope>"
       
  // Try to parse the XML string into DOM object
                xmlObj.loadXML(sXml) ;
               
  //To see the validated XML string is well-formed
               
                var xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP") ;
                xmlHTTP.Open ( "Post", serviceUrl , false) ;
                xmlHTTP.setRequestHeader("SOAPAction", "http://tempuri.org/SubmitResult") ;
                xmlHTTP.setRequestHeader("Content-Type", "text/xml; charset=utf-8" ) ;
                xmlHTTP.Send(xmlObj.xml) ;
                // Getting Result              
                var xmlResponse = xmlHTTP.responseXML ;
                if(xmlResponse != null)
               {
                    alert('Data submitted.');
               }
 

This worked well for us and client accepted the solution, if there are any other ways to do the same please do share with us, we are always willing to learn. 

No comments:

Post a Comment

How to get code from a published ASP.Net build where we don't have source code?

If you have ever lost or misplaced your source code for an ASP.Net web application, you might wonder if there is a way to recover it from th...