SlideShare a Scribd company logo
1
6-Mar-15
Ajax
Asynchronous JavaScript and XML
Introduction
Ajax (sometimes capitalized as AJAX) stands for
Asynchronous JavaScript And XML
Ajax is a technique for creating β€œbetter, faster, more
responsive web applications”
Web applications with Ajax are supposed to replace all
our traditional desktop applications
These changes are so sweeping that the Ajax-enabled
web is sometimes know as β€œWeb 2.0”
Introduction How Ajax works
1. You do something with an HTML form in your
browser
2. JavaScript on the HTML page sends an HTTP request
to the server
3. The server responds with a small amount of data,
rather than a complete web page
4. JavaScript uses this data to modify the page
You don’t have to reload the whole page like
previously!!!
2
Underlying technologies
JavaScript
HTML
CSS
XML
XML is often used for receiving data from the server
Plain text can also be used, so XML is optional
HTTP
All these are open standards
What is AJAX?
AJAX:
1) Asynchronous communication with the server
2) Retreiving data from the server (text or XML)
3) Updating the web page
1+2 = XMLHTTPRequest
3= DOM
XMLHTTPRequest :
Get data in XML, JSON or text format using HTTP
requests.
For IE7, Mozilla, OpΓ©ra, Safari …, we use
XMLHTTPRequest.
For subsequent versions of IE, we use
MicroSoft.XMLHTTP or Msxml2.XMLHTTP
we should then test the browser version before
instantiation.
Starting from the browser…
Your browser must allow JavaScript, or Ajax won’t
work
Otherwise, there’s nothing special required of the browser
Your browser has some way of providing data to the
serverβ€”usually from an HTML form
JavaScript has to handle events from the form, create
an XMLHttpRequest object, and send it (via HTTP)
to the server
Nothing special is required of the serverβ€”every server can
handle HTTP requests
Despite the name, the XMLHttpRequest object does not
require XML
3
The XMLHttpRequest object
JavaScript has to create an XMLHttpRequest object
Depending on the browser, there are three ways of doing
this
For most browsers, just do
var request = new XMLHttpRequest();
For some versions of Internet Explorer, do
var request = new ActiveXObject("Microsoft.XMLHTTP");
For other versions of Internet Explorer, do
var request = new ActiveXObject("Msxml2.XMLHTTP");
Getting the XMLHttpRequest object
function getXMLHttpRequest {
var request = false;
try { request = new XMLHttpRequest(); }
catch(err1) {
try { var request = new ActiveXObject("Microsoft.XMLHTTP"); }
catch(err2) {
try { var request = new ActiveXObject("Msxml2.XMLHTTP"); }
catch(err3) {
request = false;
}
}
}
return request;
}
Getting the XMLHttpRequest object Preparing the XMLHttpRequest object
Once you have an XMLHttpRequest object, you have
to prepare it with the open method
request.open(method, URL, asynchronous)
The method is usually 'GET' or 'POST'
The URL is where you are sending the data
If using a 'GET', data is appended to the URL
If using a 'POST', data is added in a later step
If asynchronous is true, the browser does not wait for a
response (this is what you usually want)
request.open(method, URL)
As above, with asynchronous defaulting to true
4
Sending the XMLHttpRequest object
Once the XMLHttpRequest object has been prepared, you have
to send it
request.send(null);
This is the version you use with a GET request
request.send(content);
This is the version you use with a POST request
The content has the same syntax as the suffix to a GET request
POST requests are used less frequently than GET requests
Example:
request.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
request.send('var1=' + value1 + '&var2=' + value2);
On the server side
The server gets a standard HTTP request
The response is a standard HTTP response too
Instead of returning a complete HTML page as a
response, the server returns an arbitrary text string
(possibly XML, possibly something else)
Getting the response
Ajax uses asynchronous callsβ€”you don’t wait for the response
Instead, you have to handle an event
request.onreadystatechange = someFunction;
This is a function assignment, not a function call
When the function is called, it will be called with no parameters
function someFunction() {
if(request.readyState == 4){
var response = request.responseText;
// Do something with the response
}
}
To be safe, set up the handler before you call the send function
Displaying the response
http_request.onreadystatechange =
function() { alertContents(http_request); };
http_request.open('GET', url, true);
http_request.send(null);
function alertContents(http_request) {
if (http_request.readyState == 4) { /* 4 means got the response */
if (http_request.status == 200) {
alert(http_request.responseText);
} else {
alert('There was a problem with the request.');
}
}
}
5
The readyState property
The readyState property defines the current state of the
XMLHttpRequest object.
Here are the possible values for the readyState property:
readyState=0 after you have created the XMLHttpRequest object, but
before you have called the open() method.
readyState=1 after you have called the open() method, but before you
have called send().
readyState=2 after you have called send().
readyState=3 after the browser has established a communication with
the server, but before the server has completed the response.
readyState=4 after the request has been completed, and the response
data have been completely received from the server.
Not all browsers use all states
Usually you are only interested in state 4
http://www.w3schools.com/ajax/ajax_xmlhttprequest.asp
Doing it with XML
Here’s an XML file named test.xml:
<?xml version="1.0" ?>
<root> I'm a test. </root>
Then in alertContents() from the previous slide, we need to
replace the line
alert(http_request.responseText);
with:
var xmldoc = http_request.responseXML;
var root_node =
xmldoc.getElementsByTagName('root').item(0);
alert(root_node.firstChild.data);
http://developer.mozilla.org/en/docs/AJAX:Getting_Started
innerHTML
innerHTML is a non-W3C DOM property that gets or sets the
text between start and end tags of an HTML element
When the innerHTML property is set, the given string completely replaces
the existing content of the object
If the string contains HTML tags, the string is parsed and formatted as it is
placed into the document
Syntax:
var markup = element.innerHTML;
element.innerHTML = markup;
Example:
document.getElementById(someId).innerHTML = response;
Summary
Create an XMLHttpRequest object (call it request)
Build a suitable URL, with ?var=value suffix
request.open('GET', URL)
request.onreadystatechange = handlerMethod;
request.send(null);
function alertContents() {
if (request.readyState == 4) {
// do stuff
}
}
}

More Related Content

PPTX
What is Ajax technology?
PPTX
C# Framework class library
PPT
Xampp Ppt
PDF
Spring MVC Framework
PPT
Android Application Development Using Java
PPTX
Ajax ppt - 32 slides
PPTX
Namespaces in C#
PPT
Server Controls of ASP.Net
What is Ajax technology?
C# Framework class library
Xampp Ppt
Spring MVC Framework
Android Application Development Using Java
Ajax ppt - 32 slides
Namespaces in C#
Server Controls of ASP.Net

What's hot (20)

PPT
Ajax presentation
PPTX
How Hashmap works internally in java
PPTX
Ajax presentation
PDF
Model View Controller (MVC)
PPTX
Database in Android
PDF
UDA-Componentes RUP. PestaΓ±as
PDF
react redux.pdf
PPTX
Jsp and jstl
PPT
Asynchronous JavaScript & XML (AJAX)
DOCX
Spring notes
PDF
UDA-Componentes RUP. Formulario
PDF
Embedded Android : System Development - Part IV (Android System Services)
PPTX
Asp.net and .Net Framework ppt presentation
PDF
UDA-Componentes RUP. Mantenimiento (v2.1.1 deprecado)
DOC
Asp.Net Tutorials
PPTX
Master page
PPTX
Angular 6 Form Validation with Material
ODP
Php variables (english)
PDF
CCNA LAB - Cisco Packet Tracer
PPTX
Asp.NET Validation controls
Ajax presentation
How Hashmap works internally in java
Ajax presentation
Model View Controller (MVC)
Database in Android
UDA-Componentes RUP. PestaΓ±as
react redux.pdf
Jsp and jstl
Asynchronous JavaScript & XML (AJAX)
Spring notes
UDA-Componentes RUP. Formulario
Embedded Android : System Development - Part IV (Android System Services)
Asp.net and .Net Framework ppt presentation
UDA-Componentes RUP. Mantenimiento (v2.1.1 deprecado)
Asp.Net Tutorials
Master page
Angular 6 Form Validation with Material
Php variables (english)
CCNA LAB - Cisco Packet Tracer
Asp.NET Validation controls
Ad

Viewers also liked (18)

PPTX
Cara Pemisahan menggunakan kromotografi lapis tipis
PPT
Ebecm17
PPT
Web server administration
PPTX
Ppt krbon aktif
PPTX
Metode AOP untuk Mengolah Limbah Resi Cair
PPTX
HAKI
PPTX
Panduan penulisan jurnal
PPTX
Siklus Nitrogen
PPTX
Evaluation techniques in HCI
PPTX
Pemanfaatan Batu apung sebagai Zeolit Sintesis
PPT
UCD and low-fidelity prototyping
PPTX
Kimia industri
PPTX
evaluation techniques in HCI
PPT
Web forms and server side scripting
PPTX
Ppt optimasi pembuatan vco (virgin coconut oil )
PPTX
Reaksi unsur golongan II A
PDF
Accountancy Project Class 12th CBSE
PDF
Business Studies (Principles of Management) Project Class 12th CBSE
Cara Pemisahan menggunakan kromotografi lapis tipis
Ebecm17
Web server administration
Ppt krbon aktif
Metode AOP untuk Mengolah Limbah Resi Cair
HAKI
Panduan penulisan jurnal
Siklus Nitrogen
Evaluation techniques in HCI
Pemanfaatan Batu apung sebagai Zeolit Sintesis
UCD and low-fidelity prototyping
Kimia industri
evaluation techniques in HCI
Web forms and server side scripting
Ppt optimasi pembuatan vco (virgin coconut oil )
Reaksi unsur golongan II A
Accountancy Project Class 12th CBSE
Business Studies (Principles of Management) Project Class 12th CBSE
Ad

Similar to Ajax and xml (20)

PPT
Ajax
Β 
PDF
Core Java tutorial at Unit Nexus
PPT
Ajax tutorial by bally chohan
PPT
Ajax
PPTX
Ajax
PPT
Ajax
PPT
Web Programming using Asynchronous JavaX
PPT
PHP - Introduction to PHP AJAX
PPTX
AJAX.pptx
PPTX
Unit-5.pptx
PPTX
Learn AJAX at ASIT
Β 
PPTX
Ajax Technology
PPTX
ajax - the basics
PPT
Ajax Overview by Bally Chohan
PPTX
AJAX.pptx
PPT
PPTX
HSHDGDGDHDYDUDUDUDHDHDHSHAHAHSHSBDHDHDHDHDHD
PPT
AJAX
Ajax
Β 
Core Java tutorial at Unit Nexus
Ajax tutorial by bally chohan
Ajax
Ajax
Ajax
Web Programming using Asynchronous JavaX
PHP - Introduction to PHP AJAX
AJAX.pptx
Unit-5.pptx
Learn AJAX at ASIT
Β 
Ajax Technology
ajax - the basics
Ajax Overview by Bally Chohan
AJAX.pptx
HSHDGDGDHDYDUDUDUDHDHDHSHAHAHSHSBDHDHDHDHDHD
AJAX

More from sawsan slii (8)

PPTX
Ch1 traditional it and cloud
PPTX
Aws principle services: IAM,VPC, EC2, Cloudwatch
PPTX
Introduction to exploring hci
PPTX
Design principles
PPTX
Introduction hci
PPTX
Ch1 traditional it and cloud
PPTX
Cache mapping exercises
PPTX
Fileprocessing lec-7
Ch1 traditional it and cloud
Aws principle services: IAM,VPC, EC2, Cloudwatch
Introduction to exploring hci
Design principles
Introduction hci
Ch1 traditional it and cloud
Cache mapping exercises
Fileprocessing lec-7

Recently uploaded (20)

PDF
WebRTC in SignalWire - troubleshooting media negotiation
PDF
Decoding a Decade: 10 Years of Applied CTI Discipline
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PPTX
Digital Literacy And Online Safety on internet
PPTX
presentation_pfe-universite-molay-seltan.pptx
PDF
The Internet -By the Numbers, Sri Lanka Edition
Β 
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PPTX
artificial intelligence overview of it and more
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PPTX
Internet___Basics___Styled_ presentation
PDF
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
PPT
tcp ip networks nd ip layering assotred slides
PDF
Testing WebRTC applications at scale.pdf
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
WebRTC in SignalWire - troubleshooting media negotiation
Decoding a Decade: 10 Years of Applied CTI Discipline
Introuction about WHO-FIC in ICD-10.pptx
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
Digital Literacy And Online Safety on internet
presentation_pfe-universite-molay-seltan.pptx
The Internet -By the Numbers, Sri Lanka Edition
Β 
Tenda Login Guide: Access Your Router in 5 Easy Steps
PptxGenJS_Demo_Chart_20250317130215833.pptx
artificial intelligence overview of it and more
Design_with_Watersergyerge45hrbgre4top (1).ppt
Unit-1 introduction to cyber security discuss about how to secure a system
Internet___Basics___Styled_ presentation
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
tcp ip networks nd ip layering assotred slides
Testing WebRTC applications at scale.pdf
Job_Card_System_Styled_lorem_ipsum_.pptx
introduction about ICD -10 & ICD-11 ppt.pptx
Slides PDF The World Game (s) Eco Economic Epochs.pdf
Module 1 - Cyber Law and Ethics 101.pptx

Ajax and xml

  • 1. 1 6-Mar-15 Ajax Asynchronous JavaScript and XML Introduction Ajax (sometimes capitalized as AJAX) stands for Asynchronous JavaScript And XML Ajax is a technique for creating β€œbetter, faster, more responsive web applications” Web applications with Ajax are supposed to replace all our traditional desktop applications These changes are so sweeping that the Ajax-enabled web is sometimes know as β€œWeb 2.0” Introduction How Ajax works 1. You do something with an HTML form in your browser 2. JavaScript on the HTML page sends an HTTP request to the server 3. The server responds with a small amount of data, rather than a complete web page 4. JavaScript uses this data to modify the page You don’t have to reload the whole page like previously!!!
  • 2. 2 Underlying technologies JavaScript HTML CSS XML XML is often used for receiving data from the server Plain text can also be used, so XML is optional HTTP All these are open standards What is AJAX? AJAX: 1) Asynchronous communication with the server 2) Retreiving data from the server (text or XML) 3) Updating the web page 1+2 = XMLHTTPRequest 3= DOM XMLHTTPRequest : Get data in XML, JSON or text format using HTTP requests. For IE7, Mozilla, OpΓ©ra, Safari …, we use XMLHTTPRequest. For subsequent versions of IE, we use MicroSoft.XMLHTTP or Msxml2.XMLHTTP we should then test the browser version before instantiation. Starting from the browser… Your browser must allow JavaScript, or Ajax won’t work Otherwise, there’s nothing special required of the browser Your browser has some way of providing data to the serverβ€”usually from an HTML form JavaScript has to handle events from the form, create an XMLHttpRequest object, and send it (via HTTP) to the server Nothing special is required of the serverβ€”every server can handle HTTP requests Despite the name, the XMLHttpRequest object does not require XML
  • 3. 3 The XMLHttpRequest object JavaScript has to create an XMLHttpRequest object Depending on the browser, there are three ways of doing this For most browsers, just do var request = new XMLHttpRequest(); For some versions of Internet Explorer, do var request = new ActiveXObject("Microsoft.XMLHTTP"); For other versions of Internet Explorer, do var request = new ActiveXObject("Msxml2.XMLHTTP"); Getting the XMLHttpRequest object function getXMLHttpRequest { var request = false; try { request = new XMLHttpRequest(); } catch(err1) { try { var request = new ActiveXObject("Microsoft.XMLHTTP"); } catch(err2) { try { var request = new ActiveXObject("Msxml2.XMLHTTP"); } catch(err3) { request = false; } } } return request; } Getting the XMLHttpRequest object Preparing the XMLHttpRequest object Once you have an XMLHttpRequest object, you have to prepare it with the open method request.open(method, URL, asynchronous) The method is usually 'GET' or 'POST' The URL is where you are sending the data If using a 'GET', data is appended to the URL If using a 'POST', data is added in a later step If asynchronous is true, the browser does not wait for a response (this is what you usually want) request.open(method, URL) As above, with asynchronous defaulting to true
  • 4. 4 Sending the XMLHttpRequest object Once the XMLHttpRequest object has been prepared, you have to send it request.send(null); This is the version you use with a GET request request.send(content); This is the version you use with a POST request The content has the same syntax as the suffix to a GET request POST requests are used less frequently than GET requests Example: request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); request.send('var1=' + value1 + '&var2=' + value2); On the server side The server gets a standard HTTP request The response is a standard HTTP response too Instead of returning a complete HTML page as a response, the server returns an arbitrary text string (possibly XML, possibly something else) Getting the response Ajax uses asynchronous callsβ€”you don’t wait for the response Instead, you have to handle an event request.onreadystatechange = someFunction; This is a function assignment, not a function call When the function is called, it will be called with no parameters function someFunction() { if(request.readyState == 4){ var response = request.responseText; // Do something with the response } } To be safe, set up the handler before you call the send function Displaying the response http_request.onreadystatechange = function() { alertContents(http_request); }; http_request.open('GET', url, true); http_request.send(null); function alertContents(http_request) { if (http_request.readyState == 4) { /* 4 means got the response */ if (http_request.status == 200) { alert(http_request.responseText); } else { alert('There was a problem with the request.'); } } }
  • 5. 5 The readyState property The readyState property defines the current state of the XMLHttpRequest object. Here are the possible values for the readyState property: readyState=0 after you have created the XMLHttpRequest object, but before you have called the open() method. readyState=1 after you have called the open() method, but before you have called send(). readyState=2 after you have called send(). readyState=3 after the browser has established a communication with the server, but before the server has completed the response. readyState=4 after the request has been completed, and the response data have been completely received from the server. Not all browsers use all states Usually you are only interested in state 4 http://www.w3schools.com/ajax/ajax_xmlhttprequest.asp Doing it with XML Here’s an XML file named test.xml: <?xml version="1.0" ?> <root> I'm a test. </root> Then in alertContents() from the previous slide, we need to replace the line alert(http_request.responseText); with: var xmldoc = http_request.responseXML; var root_node = xmldoc.getElementsByTagName('root').item(0); alert(root_node.firstChild.data); http://developer.mozilla.org/en/docs/AJAX:Getting_Started innerHTML innerHTML is a non-W3C DOM property that gets or sets the text between start and end tags of an HTML element When the innerHTML property is set, the given string completely replaces the existing content of the object If the string contains HTML tags, the string is parsed and formatted as it is placed into the document Syntax: var markup = element.innerHTML; element.innerHTML = markup; Example: document.getElementById(someId).innerHTML = response; Summary Create an XMLHttpRequest object (call it request) Build a suitable URL, with ?var=value suffix request.open('GET', URL) request.onreadystatechange = handlerMethod; request.send(null); function alertContents() { if (request.readyState == 4) { // do stuff } } }