Ajax
Ajax
What is AJAX
• Asynchronous JavaScript and XML.
• It is a group of inter-related technologies like JavaScript,
DOM, XML, HTML, CSS etc.
• allows you to send and receive data asynchronously
without reloading the web page. So it is fast.
• allows you to send only important information to the
server not the entire page
• gmail, facebook,twitter, google map, youtube
AJAX is not a programming language.
AJAX just uses a combination of:
1.A browser built-in XMLHttpRequest object (to request data
from a web server)
2.JavaScript and HTML DOM (to display or use the data)
Understanding Synchronous vs Asynchronous
Method Description
void open(method, URL) opens the request specifying get
or post method and url.
void open(method, URL, async) same as above but specifies
asynchronous or not.
void open(method, URL, async, same as above but specifies
username, password) username and password.
void send() sends get request.
void send(string) send post request.
setRequestHeader(header,value) it adds request headers.
Create an XMLHttpRequest Object
variable = new XMLHttpRequest();
Send a Request To a Server
use the open() and send() methods
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
Method Description
open(method, url, Specifies the type of request
async)
method: the type of request: GET or POST
url: the server (file) location
async: true (asynchronous) or false (synchronous)
</body>
</html>
<html>
<style>
table,th,td {
border : 1px solid black;
border-collapse: collapse;
}
th,td {
padding: 5px;
}
</style><body>
<h1>The XMLHttpRequest Object</h1>
<button type="button" onclick="loadDoc()">Get my CD collection</button>
<table id="demo"></table>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "cd_catalog.xml", true);
xhttp.send();
}
function myFunction(xml) {
var i;
var xmlDoc = xml.responseXML;
var table="<tr><th>Artist</th><th>Title</th></tr>";
var x = xmlDoc.getElementsByTagName("CD");
for (i = 0; i <x.length; i++) {
table += "<tr><td>" +
x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +
"</td></tr>";
}
document.getElementById("demo").innerHTML = table;
}
</script>
</body>
</html>
Callback Function
• function passed as a parameter to another function
• you have more than one AJAX task in a website, you should
create one function for executing the XMLHttpRequest
object, and one callback function for each AJAX task.
• function call should contain the URL and what function to
call when the response is ready
Server Response Properties
Property Description
responseText get the response data as a string
returns the server response as a JavaScript string
document.getElementById("demo").innerHTML =
xhttp.responseText;
responseXML get the response data as XML data
returns the server response as an XML DOM object
loadDoc("url-1", myFunction1);
loadDoc("url-2", myFunction2);
function loadDoc(url, cFunction) {
var xhttp;
xhttp=new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
cFunction(this);
}
};
xhttp.open("GET", url, true);
xhttp.send();
}
function myFunction1(xhttp) {
// action goes here
}
function myFunction2(xhttp) {
// action goes here
}
WEB SERVICES
What is a web service
• A web service is any piece of software that makes itself
available over the internet and uses a standardized XML
messaging system.
• Web services are self-contained, modular, distributed,
dynamic applications that can be described, published, located,
or invoked over the network to create products, processes, and
supply chains.
• A web service is a collection of open protocols and standards
used for exchanging data between applications or systems
• Software Component stored on one computer that can be
accessed via method calls by an application on another
computer
A Web Service is can be defined by following ways:
1.is a client server application or application component for communication.
2.method of communication between two devices over network.
3.is a software system for interoperable machine to machine communication.
4.is a collection of standards or protocols for exchanging information between two
devices or application.
WSDL
WSDL is an acronym for Web Services Description Language.
WSDL is a xml document containing information about web services such as method
name, method parameter and how to access it.
WSDL is a part of UDDI. It acts as a interface between web service applications.
WSDL is pronounced as wiz-dull.
UDDI
UDDI is an acronym for Universal Description, Discovery and Integration.
UDDI is a XML based framework for describing, discovering and integrating web
services.
UDDI is a directory of web service interfaces described by WSDL, containing
information about web services.
10
23
SOAP
SOAP is an acronym for Simple Object Access Protocol.
SOAP is a XML-based protocol for accessing web services.
SOAP is a W3C recommendation for communication between applications.
SOAP is XML based, so it is platform independent and language independent. In
other words, it can be used with Java, .Net or PHP language on any platform.
Advantages Disadvantages
WS Security: SOAP defines its Slow: SOAP uses XML format that must
own security known as WS be parsed to be read. It defines many
Security. standards that must be followed while
Language and Platform developing the SOAP applications. So it is
independent: SOAP web services slow and consumes more bandwidth and
can be written in any programming resource.
language and executed in any WSDL dependent: SOAP uses WSDL
platform. and doesn't have any other mechanism to
discover the service.
RESTful Web Services
1,143
JAVA Web SERVICES
web services resides at server side
client requests for web service and get from remote machine
through response
<SOAP-ENV:Header>
...
...
</SOAP-ENV:Header>
<SOAP-ENV:Body>
...
...
<SOAP-ENV:Fault>
...
...
</SOAP-ENV:Fault>
...
</SOAP-ENV:Body>
</SOAP_ENV:Envelope>
The SOAP envelope contains two parts:
1.An optional header providing information on authentication, encoding of
data, or how a recipient of a SOAP message should process the message.
2.The body that contains the message. These messages can be defined using
the WSDL specification.
SOAP Request
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-
ENV="http://www.w3.org/2001/12/soap-envelope" SOAP-
ENV:encodingStyle="http://www.w3.org/2001/12/soap-encoding" >
<m:GetQuotation>
<m:QuotationsName>MiscroSoft</m:QuotationsName>
</m:GetQuotation>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOAP Response
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-
ENV="http://www.w3.org/2001/12/soap-envelope" SOAP-
ENV:encodingStyle="http://www.w3.org/2001/12/soap-encoding" >
<m:GetQuotationResponse>
<m:Quotation>Here is the quotation</m:Quotation>
</m:GetQuotationResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
6