Consuming .NET Webservice by Javascript

25. December 2009

I have a webService service.asmx and I would like to call and consum this webservice using XMLHttpRequest and SOAP. The thing why I would like to do this is that I have hosted a client gadget application which is accessing the webservice perfectly.

Calling the Service from javascript:

  var req;


  if(window.XMLHttpRequest){
  //For Firefox, Safari, Opera
      req = new XMLHttpRequest();
  }
  else if(window.ActiveXObject){
  //For IE 5
      req = new ActiveXObject("Microsoft.XMLHTTP");
  } else if(window.ActiveXObject){
  //For IE 6+
      req = new ActiveXObject("Msxml2.XMLHTTP");
  } else{
  //Error for an old browser
      alert('Your browser is not IE 5 or higher, or Firefox or Safari or Opera');
  }

if (req) {
    req.onreadystatechange = function() {
        if (req.readyState == 4) // 4 = "loaded"
        {
            if (req.status == 200) // 200 = OK
            {
                // XML Struktur
                var xmldata = req.responseXML;
                // Erstes Tag holen
                var FirstTag = xmldata.getElementsByTagName('Donation');
                // Vom ersten Tag die Subtags ziehen
                var SubstringTags = FirstTag[0].getElementsByTagName('string');
                // Alle subtags durchgehen und dessen Inhalt in variablen speichern
                for (i = 0; i < SubstringTags.length; i++) {
                    // Hier wird der Text aus dem string Tag gelesen
                    Array.push(SubstringTags[i].text);
                }
               
                // Anderes Tag aus der XML Struktur holen
                // Wichtig ist hier, das der Knoten Wert ausgelesen wird
                header = xmldata.getElementsByTagName('Header')[0].firstChild.nodeValue;
            }
            else {
                showError("Problem retrieving XML data");
            }
        }
    }
    req.open("GET", "service.asmx", true);
    req.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req.send(null);

 





Bookmark it!

.NET, ajax, C#, javascript, WCF, webservice, XML


Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading