Exforsys

Home arrow Technical Training arrow Ajax Tutorial

Ajax, Web Services & XML Part II Page - 2

Page 2 of 2
Author : Exforsys Inc.     Published on: 16th Jul 2006

Ajax, Web Services & XML Part II

Ads

Functions Used:

Sample Code
  1.  
  2. function makeCall(url, callback, xml) {
  3.   request = new ActiveXObject("Microsoft.XMLHTTP");
  4.   if (request) {
  5.     var now = new Date();
  6.     request.onreadystatechange = callback;
  7.     request.open("POST", url, true);
  8.     request.setRequestHeader("SOAPAction", "<a href="http://www.ignyte.com/whatsshowing/GetTheatersAndMovies" target="_blank" rel="nofollow">http://www.ignyte.com/whatsshowing/GetTheatersAndMovies</a>");
  9.     request.setRequestHeader("User-Agent", "Mindreef SOAPscope 4.1.2000 (<a href="http://www.mindreef.com/" target="_blank" rel="nofollow">http://www.mindreef.com</a>")
  10.     request.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
  11.     request.send(xml);
  12.   }
  13. }
  14.  
  15. function theaters() {
  16.   var zip = document.getElementById("zip").value;
  17.   var radius = document.getElementById("radius").value;
  18.   xml = '<?xml version="1.0" encoding="UTF-8"?>';
  19.   xml += '<soap:Envelope ';
  20.   xml += 'xmlns:soap="<a href="http://schemas.xmlsoap.org/soap/envelope/" target="_blank" rel="nofollow">http://schemas.xmlsoap.org/soap/envelope/</a>" ';
  21.   xml += 'xmlns:tns="<a href="http://www.ignyte.com/whatsshowing" target="_blank" rel="nofollow">http://www.ignyte.com/whatsshowing</a>" ';
  22.   xml += 'xmlns:xs="<a href="http://www.w3.org/2001/XMLSchema" target="_blank" rel="nofollow">'">http://www.w3.org/2001/XMLSchema" target="_blank" rel="nofollow">'</a>;
  23.  xml += '   <soap:Body>';
  24.  xml += '      <tns:GetTheatersAndMovies>';
  25.  xml += '         <tns:zipCode>'+zip+'</tns:zipCode>';
  26.  xml += '         <tns:radius>'+radius+'</tns:radius>';
  27.  xml += '      </tns:GetTheatersAndMovies>';
  28.  xml += '   </soap:Body>';
  29.  xml += '</soap:Envelope>';
  30.  document.getElementById("request").value = xml;
  31.  makeCall('http://www.ignyte.com/webservices/ignyte.whatsshowing.webservice/moviefunctions.asmx" target="_blank" rel="nofollow"', updateMe, xml);
  32. }
  33.  
  34. function updateMe() {
  35.   if (request.readyState != 4) return;
  36.   if (request.status != 200) return;
  37.   html = "";
  38.   var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  39.   xmlDoc.loadXML(request.responseText);
  40.   dfs(xmlDoc);
  41.   document.getElementById("request").value = request.responseText;
  42.   document.getElementById("result").innerHTML = html;
  43.   request.abort();
  44.   request = null;
  45. }
  46.  
  47. function dfs(node) {
  48.   var stuff = "";
  49.   for(var i=0; i<node.childNodes.length; i++) {
  50.     var thisNode = node.childNodes(i);
  51.     if (thisNode.nodeName == "Theater") {
  52.       html += theater(thisNode) + "n";
  53.     } else {
  54.       dfs(thisNode);
  55.     }
  56.   }
  57.   return stuff;
  58. }
  59.  
  60. function theater(node) {
  61.   var stuff = "";
  62.   for(var i=0; i<node.childNodes.length; i++) {
  63.     var thisNode = node.childNodes(i);
  64.     if (thisNode.nodeName == "Name") {
  65.       stuff += "<h1>"+thisNode.childNodes(0).nodeValue+"</h1>n";
  66.     } else if (thisNode.nodeName == "Address") {
  67.       stuff += "<h2>"+thisNode.childNodes(0).nodeValue+"</h2>n";
  68.     } else if (thisNode.nodeName == "Movies") {
  69.       stuff += movies(thisNode);
  70.     }
  71.   }
  72.   return stuff;
  73. }
  74.  
  75. function movies(node) {
  76.   var stuff = "<table border=1>";
  77.   for(var i=0; i<node.childNodes.length; i++) {
  78.     var thisNode = node.childNodes(i);
  79.     if (thisNode.nodeName == "Movie") {
  80.       stuff += "<tr>"+movie(thisNode)+"</tr>";
  81.     }
  82.   }
  83.   return stuff+"</table>";
  84. }
  85.  
  86. function movie(node) {
  87.   var stuff = "";
  88.   for(var i=0; i<node.childNodes.length; i++) {
  89.     var thisNode = node.childNodes(i);
  90.     if (thisNode.nodeName == "Rating") {
  91.       stuff += "<td>"+thisNode.childNodes(0).nodeValue+"</td>";
  92.     } else if (thisNode.nodeName == "Name") {
  93.       stuff += "<td>"+thisNode.childNodes(0).nodeValue+"</td>";
  94.     } else if (thisNode.nodeName == "RunningTime") {
  95.       stuff += "<td>"+thisNode.childNodes(0).nodeValue+"</td>";
  96.     } else if (thisNode.nodeName == "ShowTimes") {
  97.       stuff += "<td>"+thisNode.childNodes(0).nodeValue+"</td>";
  98.     }
  99.   }
  100.   return stuff;
  101. }
Copyright exforsys.com


Download Sample Code

Author: Greg Smith



 
This tutorial is part of a Ajax Tutorial tutorial series. Read it from the beginning and learn yourself.

Ajax Tutorial

 

Comments