Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

Ajax, Web Services & XML Part I

By Exforsys | on July 16, 2006 |
Ajax Tutorial

Last time we looked at Ajax and how to create a remote call to a service. We passed no parameters and received a simple text string. In this installment, we look at calling a web service using the SOAP protocol and XML. The response will come back to us in an XML document which we will display in a textarea.

As explained in the article “Introduction to Web Services” , a web service is basically a web site that receives a request and processes it and returns XML instead of HTML. The XML must have a format that can be interpreted as a web service call – this format is SOAP.

SOAP is a simple wrapper or “envelope” around an XML document. It has an optional header and a required body. The header contains information about the request/response and the body contains the bulk of the message. Our basic request to a movies database web service looks like this:

  1. <!--l version="1.0" encoding="UTF-8-->
  2. <!--l version="1.0" encoding="UTF-8-->
  3. <?xml version="1.0" encoding="UTF-8"?<
  4. <soap:Envelope xmlns:soap="<a href="http://schemas.xmlsoap.org/soap/envelope/" target="_blank" rel="nofollow">
  5. http://schemas.xmlsoap.org/soap/envelope/</a>" 
  6. xmlns:tns="<a href="http://www.ignyte.com/whatsshowing" target="_blank" rel="nofollow">
  7. http://www.ignyte.com/whatsshowing</a>" 
  8. xmlns:xs="<a href="http://www.w3.org/2001/XMLSchema" target="_blank" rel="nofollow">
  9. http://www.w3.org/2001/XMLSchema</a>"<
  10.  <soap:Body<
  11.  <tns:GetTheatersAndMovies<
  12.  <tns:zipCode<23112</tns:zipCode<
  13.  <tns:radius<12</tns:radius<
  14.  </tns:GetTheatersAndMovies<
  15.  </soap:Body<
  16. </soap:Envelope<<!--l version="1.0" encoding="UTF-8-->

This request generates a response that lists area theaters and the movies and times they run.

What is significant about this example is that we have no back-end code supporting the application. The call to the web service is completely contained within the web page. We don’t need to write any additional code to support the application.

This also means that there is no “round tripping” to the web- or app-server. The web page will not refresh. The response will go out to the web service and return to our browser and the results will be displayed in the textarea.

Take the time to check out file ajax2a.html and run the application. (This example is written for IE6) Enter a zip code and a radius to search. When you click the button, the values will be sent to the web service. The SOAP request will be displayed in the first textarea and the SOAP response will be displayed in the second one.

When you look at the code, you’ll see similar features as in the last article. Function makeCall() now takes on three parameters – a URL, a Callback, and an XML document. This generalizes the call significantly. We also are using a POST, so that the XML payload can be delivered (instead of parameters in the URL). We are setting some header values to allow the XML to pass through the Internet and be received by the Web Service. Finally, when we send the request, the XML is specified as a payload for the request.

A new function “theaters()” creates the SOAP request and inserts the zip code and radius into the middle of it. Then it copies the XML into the response textarea and calls makeCall() with updateMe() as the callback procedure. UpdateMe() is unchanged from last time. When UpdateMe() is called, it fills in the response textarea with the SOAP response.

  1. function theaters() {
  2. var zip = document.getElementById("zip").value;
  3. var radius = document.getElementById("radius").value;
  4. var xml = '<?xml version="1.0" encoding="UTF-8"?<';
  5. xml += '<soap:Envelope ';
  6. xml += 'xmlns:soap="</font>
  7. <a href="http://schemas.xmlsoap.org/soap/envelope/" target="_blank" rel="nofollow">
  8. <font color="#800000" size="3">http://schemas.xmlsoap.org/soap/envelope/</font></a>
  9. <font color="#800000" size="3">" ';
  10. xml += 'xmlns:tns="</font>
  11. <a href="http://www.ignyte.com/whatsshowing" target="_blank" rel="nofollow">
  12. <font color="#800000" size="3">
  13. http://www.ignyte.com/whatsshowing" target="_blank" rel="nofollow"</font></a>
  14. <font color="#800000" size="3">" ';
  15. xml += 'xmlns:xs="</font>
  16. <a href="http://www.w3.org/2001/XMLSchema" target="_blank" rel="nofollow"<'">
  17. <font color=" #800000" size="3">
  18. http://www.w3.org/2001/XMLSchema" target="_blank" rel="nofollow"<'</font></a>
  19. <font color="#800000" size="3">;
  20. xml += ' 
  21. <soap:Body<';
  22. xml += ' <tns:GetTheatersAndMovies<';
  23. xml += ' <tns:zipCode<'+zip+'</tns:zipCode<';
  24. xml += ' <tns:radius<'+radius+'</tns:radius<';
  25. xml += ' </tns:GetTheatersAndMovies<';
  26. xml += ' </soap:Body<';
  27. xml += '</soap:Envelope<';
  28. document.getElementById("request").value = xml;
  29. makeCall('http://www.ignyte.com/webservices/ignyte.whatsshowing.webservice/moviefunctions.asmx" 
  30. target="_blank" rel="nofollow"', updateMe, xml);
  31. }

If you look closely at the SOAP request, you can see that it contains the tags

  1.  <tns:zipCode<23112</tns:zipCode<
  1.  <tns:radius<15</tns:radius<.
These are the input parameters.

And looking at the SOAP response (which could be huge), you will see a large number of tags like this:

  1.  <Theater
  2. 	<<Name<Commonwealth 20</Name
  3. <</Theater<
and
  1.  <Movie<<Rating<PG-13</Rating
  2. 	<<Name<A Prairie Home Companion</Name< 
  3. 	<RunningTime<1 hr 45 mins</RunningTime< 
  4. 	<ShowTimes<1:25pm | 4:00pm | 6:30pm | 9:15pm</ShowTimes<
  5. </Movie<.

The values should be self descriptive.

You can fill your web page with many such calls. Since the calls are “asynchronous”, your web page will not “freeze” as the call is satisfied across the network. Instead, you will be able to continue to scroll and click on other controls – even spawn off other web service calls. When the data finally returns from the web service, the callback function is called without any further action on the part of the user or the JavaScript code.

This can be a problem. If a web service takes too long to respond it will time out and the callback will get an error code. Also, the programmer must take care not to destroy data thanks to a callback. For example, in our program, if the user starts to type in the response textarea, when the callback executes, the user’s entries will be destroyed.

The ability to call out to web services from a HTML page allows the programmer to take advantage of a myriad of free services on the internet. When combined with custom web services, powerful back end logic can be called quickly and easily from the Rich Client front end.

Other Functions Used :

  1. function makeCall(url, callback, xml) {
  2.  request = new ActiveXObject("Microsoft.XMLHTTP");
  3.  if (request) {
  4.  var now = new Date();
  5.  request.onreadystatechange = callback;
  6.  request.open("POST", url, true);
  7.  request.setRequestHeader("SOAPAction", "</font>
  8. <a href="http://www.ignyte.com/whatsshowing/GetTheatersAndMovies" target="_blank" rel="nofollow">
  9. <font color="#800000" size="3">
  10. http://www.ignyte.com/whatsshowing/GetTheatersAndMovies" target="_blank" rel="nofollow"
  11. </font>
  12. </a>
  13. <font color="#800000" size="3">");
  14.  request.setRequestHeader("User-Agent", "Mindreef SOAPscope 4.1.2000 (</font>
  15. <a href="http://www.mindreef.com" target="_blank" rel="nofollow">
  16. <font color="#800000" size="3">http://www.mindreef.com" target="_blank" rel="nofollow"
  17. </font>
  18. </a>
  19. <font color="#800000" size="3">")
  20.  request.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
  21.  request.send(xml);
  22.  }
  23. }
  24.  
  25. function updateMe() {
  26.  if (request.readyState != 4) return;
  27.  if (request.status != 200) return;
  28.  document.getElementById("result").value = request.responseText;
  29.  request.abort();
  30.  request = null;
  31. }

Next time we’ll actually parse the XML response into something that can be displayed in a table.

Download Sample Code

Author: Greg Smith

« « Avoid Getting Fired From Your Old Job While looking for new one
Ajax, Web Services & XML Part II » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 391 Followers
  • Popular
  • Recent
  • Ajax Graphics Enhancement Tools

    April 5, 2007 - 0 Comment
  • Introduction to Ajax

    June 13, 2006 - 0 Comment
  • The Power of Ajax

    April 16, 2007 - 0 Comment
  • Ajax, Web Services & XML Part II

    July 16, 2006 - 0 Comment
  • Making Ajax Accessible

    March 14, 2007 - 0 Comment
  • Ajax Challenges

    March 14, 2007 - 0 Comment
  • Ajax Framework

    March 17, 2007 - 0 Comment
  • The Security Aspects of Ajax

    March 22, 2007 - 0 Comment
  • Advantages and Disadvantages of Ajax

    March 24, 2007 - 0 Comment
  • How Ajax Has Become More Standardized

    March 26, 2007 - 0 Comment
  • The Power of Ajax

    April 16, 2007 - 0 Comment
  • Ajax Graphics Enhancement Tools

    April 5, 2007 - 0 Comment
  • How Ajax Can Become as User Friendly as Flash

    March 30, 2007 - 0 Comment
  • How Ajax Can Improve Web Applications

    March 29, 2007 - 0 Comment
  • How Ajax Has Become More Standardized

    March 26, 2007 - 0 Comment
  • Advantages and Disadvantages of Ajax

    March 24, 2007 - 0 Comment
  • The Security Aspects of Ajax

    March 22, 2007 - 0 Comment
  • Ajax Framework

    March 17, 2007 - 0 Comment
  • Ajax Challenges

    March 14, 2007 - 0 Comment
  • Making Ajax Accessible

    March 14, 2007 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • The Power of Ajax
  • Ajax Graphics Enhancement Tools
  • How Ajax Can Become as User Friendly as Flash
  • How Ajax Can Improve Web Applications
  • How Ajax Has Become More Standardized

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT