pellekrogholt

Untitled

Apr 7th, 2011
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.05 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <title>Calling a Web service using Ajax functionality - jquery version</title>
  5.  
  6.     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
  7.  
  8.     <script type="text/javascript" language="javascript">
  9.  
  10.     // for: https://blog.itu.dk/SMDS-F2011/course-plan/lab-exercise-week-10
  11.  
  12.     // js based on http://openlandscape.wordpress.com/2009/09/25/call-soap-xm-web-services-with-jquery-ajax
  13.    
  14.     // various posts on the options error: com.sun.xml.internal.ws.transport.http.server.WS HttpHandler handleExchange WARNING: Cannot handle HTTP method: OPTIONS
  15.     // - http://stackoverflow.com/questions/5417014/soap-web-service-calls-from-javascript
  16.     // - http://bugs.jquery.com/attachment/ticket/6029/jquery-disable-firefox3-cross-domain-magic.patch - (though rejected by the jquery people)
  17.     // - http://stackoverflow.com/questions/1099787/jquery-ajax-post-sending-options-as-request-method-in-firefox
  18.     //
  19.     // but is probably more related to the HttpHandler at the webservice, sounds odd if options request are not allowed ?
  20.     //
  21.  
  22.     // todo: the soap stuff could probably be handled easier with http://plugins.jquery.com/project/jqSOAPClient the documnettaion/example page is unfortunately 404
  23.  
  24.  
  25.     var serviceUrl = 'http://localhost:8085/labexercises/hello'; // Preferably write this out from server side
  26.  
  27.     function call_hello_world(text)
  28.     {        
  29.         var soapMessage =
  30.         '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">' +
  31.                                 '<SOAP-ENV:Header/>' +
  32.                                 "<SOAP-ENV:Body>" +
  33.                                 '<itu:helloOperation xmlns:itu="http://hello.samples.smds2011.itu.dk/">' +
  34.                                 "<arg0>" + text + "</arg0>" +
  35.                                 "</itu:helloOperation>" +
  36.                                 "</SOAP-ENV:Body>" +
  37.                                 "</SOAP-ENV:Envelope>";
  38.  
  39.         $.ajax({
  40.             url: serviceUrl,
  41.             type: "POST",
  42.             dataType: "xml",
  43.             data: soapMessage,
  44.             complete: hello_world_state_changed,
  45.             contentType: "text/xml; charset=\"utf-8\""
  46.         });
  47.  
  48.         return false;
  49.     }
  50.  
  51.     function hello_world_state_changed(xmlHttpRequest, status)
  52.     {
  53.  
  54.         if (status != "success") {
  55.             alert("Something is seriously wrong check the log at the webservice probably that nasty 'WARNING: Cannot handle HTTP method: OPTIONS'");            
  56.         }
  57.  
  58.         if (xmlHttpRequest.readyState == 4) {
  59.             if (xmlHttpRequest.status == 200) {
  60.                 var xmlDoc = xmlHttpRequest.responseXML;
  61.                 var text = xmlDoc.getElementsByTagName("return")[0].childNodes[0].nodeValue;
  62.                 $("#textareahelloworld").val(text);
  63.             }
  64.             else
  65.             {
  66.                 alert('Call to HelloService failed! HTTP Status Code:' + xmlHttpRequest.status);
  67.                 $("#textareahelloworld").val(xmlHttpRequest.responseText);
  68.             }
  69.         }
  70.  
  71.     }    
  72.    
  73.     $(document).ready(function() {
  74.  
  75.         $('#buttonHelloWorld').click(function() {
  76.            
  77.             var text = $("#textyourname").val();
  78.             call_hello_world(text);
  79.         });
  80.  
  81.     });
  82.    
  83.  
  84.     </script>
  85.  
  86. </head>
  87. <body>
  88.    
  89.     <!-- todo: add support alternative option/version when javascript is disabled -->
  90.    
  91.     <form action="" id="myform">
  92.         <label for="textyourname">Your Name</label> <input type="text" id="textyourname"  />
  93.        
  94.         <input id="buttonHelloWorld" type="button" value="Call Hello World" />
  95.         <div>
  96.             <label for="textareahelloworld">Hello World web method response</label>
  97.             <textarea id="textareahelloworld" cols="30" rows="5"></textarea>
  98.         </div>
  99.     </form>
  100.    
  101. </body>
  102. </html>
Advertisement
Add Comment
Please, Sign In to add comment