SlideShare a Scribd company logo
Introduction to JSON & Ajax
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
2
• JSON is a syntax for storing and exchanging data
• JSON is an easier-to-use alternative to XML
• JSON is language independent
• JSON uses JavaScript syntax, but the JSON format is text
only, just like XML
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
3
JSON
XML
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
4
Similarities
• Both JSON and XML are
hierarchical (values within values)
• Both JSON and XML can be
parsed by any programming
language
• Both JSON and XML can be
fetched with an XMLHttpRequest
Differences
• JSON doesn't use end tag
• JSON is shorter
• JSON is quicker to read and
write
• JSON can use arrays
• XML has to be parsed with
an XML parser. JSON can be
parsed by a standard
JavaScript function
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
5
• JSON syntax is derived from JavaScript object notation
• Example:
• Javascript Object
• JSON Object
• JSON Text/String
• JSON Array
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
6
• Data is in name/value pairs
• name and value are separated by “:” and field name is double
quoted
• Value can be
• A number (integer or floating point)
• A string (in double quotes)
• A Boolean (true or false)
• An array (in square brackets)
• An object (in curly braces)
• null
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
7
• The JavaScript function JSON.parse(text) can be used to
convert a JSON text into a JavaScript object:
• The JavaScript function JSON.stringify(object) can be used to
convert JavaScript object to JSON String
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
8
• A common use of JSON is to exchange data between the
client and a web server
• Objects in PHP can be converted into JSON by using the PHP
function json_encode()
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
9
Asynchronous JavaScript and XML
• AJAX allows web pages to be updated asynchronously by
exchanging small amounts of data with the server behind the scenes
• With Ajax you can:-
• Update a web page without reloading the page
• Request data from a server - after the page has loaded
• Receive data from a server - after the page has loaded
• Send data to a server - in the background
• Examples of applications using AJAX:
• Google Maps, Gmail, YouTube, and Facebook.
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
10
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
11
• Many of the tasks performed on the server are very time consuming.
Before AJAX, retrieving data from server behind the scenes could
cause the application to hang or stop
• With AJAX, the JavaScript does not have to wait for the server
response, but can instead:
• execute other scripts while waiting for server response
• deal with the response when the response ready
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
12
XMLHttpRequest Object
• The keystone of AJAX is the XMLHttpRequest object
• The XMLHttpRequest object is used to exchange data with a
server behind the scenes
• Syntax for creating an XMLHttpRequest object:
• var xhttp = new XMLHttpRequest();
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
13
Send a Request to a Server
• To send a request to a server, the open() and send() methods
of the XMLHttpRequest object are used:
Method Description
open(method, url,
async)
Specifies the type of request
method: the type of request: GET or POST
url: the server (file) location
async: true (asynchronous) or false
(synchronous)
send() Sends the request to the server (used for GET)
send(string) Sends the request to the server (used for POST)
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
14
Send a Request to a Server …
• xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
• xhttp.open("GET", “reg.php?fname=Henry&lname=Ford", true);
xhttp.send();
• xhttp.open("POST", "demo_post.php", true);
xhttp.send();
• To POST form, add an HTTP header with setRequestHeader()
xhttp.open("POST", "ajax_test.asp", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-
urlencoded");
xhttp.send("fname=Henry&lname=Ford");
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
15
AJAX - Server Response
• The response from a server, can be accessed through the
responseText or responseXML property of the
XMLHttpRequest object
• responseText - get the response data as a string
• responseXML - get the response data as XML data
• document.getElementById("demo").innerHTML =
xhttp.responseText;
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
16
• The readyState property holds the status of the
XMLHttpRequest: Changes from 0 to 4
• 0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready
• The onreadystatechange event is triggered every time the
readyState changes.
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
17
• onreadystatechange event: specify what will happen when
the server response is ready to be processed
• When readyState is 4 and status is 200, the response is ready
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
18
• If you have more than one AJAX task on your
website, you should create ONE standard function
for creating the XMLHttpRequest object, and call
this for each AJAX task
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
19
• jQuery provides several methods for AJAX
functionality.
• With the jQuery AJAX methods, you can request
text, HTML, XML, or JSON from a remote server
using both HTTP Get and HTTP Post
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
20
• The load() method loads data from a server and puts the
returned data into the selected element
• Syntax
• $(selector).load(URL,data,callback);
• URL: required parameter, specifies the URL you wish to load.
• data: optional, specifies a set of querystring key/value pairs to
send along with the request.
• callback: optional, is the name of a function to be executed after
the load() method is completed.
• $("#div1").load("demo_test.txt");
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
21
• specifies a callback function to run when the load() method is
completed
• The callback function can have different parameters:
• responseTxt - contains the resulting content if the call
succeeds
• statusTxt - contains the status of the call
• xhr - contains the XMLHttpRequest object
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
22
• The jQuery get() and post() methods are used to request data
from the server with an HTTP GET or POST request
• GET is used for just getting data from the server
• Note: The GET method may return cached data
• POST can also be used to get some data from the server.
However, the POST method NEVER caches data, and is often
used to send data along with the request.
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
23
• The $.get() method requests data from the server with an HTTP
GET request.
• Syntax
• $.get(URL,callback);
• URL : required parameter, specifies the URL you wish to
request.
• callback: optional parameter , is the name of a function to be
executed if the request succeeds. Has two parameters:
• Data: holds the content of the page requested
• Status: holds the status of the request
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
24
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
25
• The $.post() method requests data from the server with an HTTP
POST request.
• Syntax
• $.get(URL,data, callback);
• URL : required parameter, specifies the URL you wish to
request.
• data: optional, specifies some data to send along with the request
• callback: optional parameter , is the name of a function to be
executed if the request succeeds. Has two parameters:
• Data: holds the content of the page requested
• Status: holds the status of the request
C O M P I L E D B Y : S E B L E N .
D E P A R T M E N T O F C O M P U T E R S C .
A D D I S A B A B A U N I V E R S I T Y
26
Ad

Recommended

PDF
Kibana: Real-World Examples
Salvatore Cordiano
 
PDF
Search Engine-Building with Lucene and Solr, Part 2 (SoCal Code Camp LA 2013)
Kai Chan
 
PDF
Search Engine-Building with Lucene and Solr, Part 1 (SoCal Code Camp LA 2013)
Kai Chan
 
PPTX
Java Performance Tips (So Code Camp San Diego 2014)
Kai Chan
 
PDF
Search Engine-Building with Lucene and Solr
Kai Chan
 
PDF
A Mapping-based Method to Query MongoDB Documents with SPARQL
Franck Michel
 
PPT
J s-o-n-120219575328402-3
Ramamohan Chokkam
 
PPT
Javascript2839
Ramamohan Chokkam
 
PDF
Indexing and Performance Tuning
MongoDB
 
PDF
The Lonesome LOD Cloud
Ruben Verborgh
 
PDF
Jsonsaga 100605143125-phpapp02
Ramamohan Chokkam
 
PDF
Json the-x-in-ajax1588
Ramamohan Chokkam
 
PPTX
Web data from R
schamber
 
PDF
Concise at NTU Graduate Institute of Linguistics
kuanming
 
PPT
Xcap tutorial
wanglixue
 
PDF
Introduction to solr
Sematext Group, Inc.
 
PDF
Linked Data Fragments
Ruben Verborgh
 
PPTX
The Aggregation Framework
MongoDB
 
PDF
よく使うテストヘルパーの紹介 #ios_test_night
Kenji Tanaka
 
PDF
useR! 2012 Talk
rtelmore
 
PDF
MongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
MongoDB
 
PDF
MongoDB Europe 2016 - Graph Operations with MongoDB
MongoDB
 
PDF
Analytics with MongoDB Aggregation Framework and Hadoop Connector
Henrik Ingo
 
PPT
Introduction to couch_db
Romain Testard
 
PDF
Getting started with apache solr
Humayun Kabir
 
PDF
Data Processing and Aggregation with MongoDB
MongoDB
 
PDF
Json at work overview and ecosystem-v2.0
Boulder Java User's Group
 
PPTX
Querying the Web of Data
Rinke Hoekstra
 
PPTX
Web technologies-course 10.pptx
Stefan Oprea
 
PDF
Introduction to AJAX
Abzetdin Adamov
 

More Related Content

What's hot (20)

PDF
Indexing and Performance Tuning
MongoDB
 
PDF
The Lonesome LOD Cloud
Ruben Verborgh
 
PDF
Jsonsaga 100605143125-phpapp02
Ramamohan Chokkam
 
PDF
Json the-x-in-ajax1588
Ramamohan Chokkam
 
PPTX
Web data from R
schamber
 
PDF
Concise at NTU Graduate Institute of Linguistics
kuanming
 
PPT
Xcap tutorial
wanglixue
 
PDF
Introduction to solr
Sematext Group, Inc.
 
PDF
Linked Data Fragments
Ruben Verborgh
 
PPTX
The Aggregation Framework
MongoDB
 
PDF
よく使うテストヘルパーの紹介 #ios_test_night
Kenji Tanaka
 
PDF
useR! 2012 Talk
rtelmore
 
PDF
MongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
MongoDB
 
PDF
MongoDB Europe 2016 - Graph Operations with MongoDB
MongoDB
 
PDF
Analytics with MongoDB Aggregation Framework and Hadoop Connector
Henrik Ingo
 
PPT
Introduction to couch_db
Romain Testard
 
PDF
Getting started with apache solr
Humayun Kabir
 
PDF
Data Processing and Aggregation with MongoDB
MongoDB
 
PDF
Json at work overview and ecosystem-v2.0
Boulder Java User's Group
 
PPTX
Querying the Web of Data
Rinke Hoekstra
 
Indexing and Performance Tuning
MongoDB
 
The Lonesome LOD Cloud
Ruben Verborgh
 
Jsonsaga 100605143125-phpapp02
Ramamohan Chokkam
 
Json the-x-in-ajax1588
Ramamohan Chokkam
 
Web data from R
schamber
 
Concise at NTU Graduate Institute of Linguistics
kuanming
 
Xcap tutorial
wanglixue
 
Introduction to solr
Sematext Group, Inc.
 
Linked Data Fragments
Ruben Verborgh
 
The Aggregation Framework
MongoDB
 
よく使うテストヘルパーの紹介 #ios_test_night
Kenji Tanaka
 
useR! 2012 Talk
rtelmore
 
MongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
MongoDB
 
MongoDB Europe 2016 - Graph Operations with MongoDB
MongoDB
 
Analytics with MongoDB Aggregation Framework and Hadoop Connector
Henrik Ingo
 
Introduction to couch_db
Romain Testard
 
Getting started with apache solr
Humayun Kabir
 
Data Processing and Aggregation with MongoDB
MongoDB
 
Json at work overview and ecosystem-v2.0
Boulder Java User's Group
 
Querying the Web of Data
Rinke Hoekstra
 

Similar to Introduction to JSON & Ajax (20)

PPTX
Web technologies-course 10.pptx
Stefan Oprea
 
PDF
Introduction to AJAX
Abzetdin Adamov
 
PPTX
AJAX and JSON
Julie Iskander
 
PPTX
Web-Engineering-Lec-14 (1) .pptx
iamayesha2526
 
PPTX
Web-Engineering-Lec-14 (1 ).pptx
iamayesha2526
 
PPT
Web Programming using Asynchronous JavaX
SivanN6
 
PDF
AJAX - An introduction
Eleonora Ciceri
 
PPTX
WEB TECHNOLOGY Unit-5.pptx
karthiksmart21
 
PPTX
Ajax
Tech_MX
 
PDF
How to make Ajax work for you
Simon Willison
 
PDF
Introduction to Ajax programming
Fulvio Corno
 
PPTX
AJAX.pptx
Ganesh Chavan
 
PDF
Lec 7
maamir farooq
 
PPTX
Unit-5.pptx
itzkuu01
 
PDF
Ajax
soumya
 
PPTX
HSHDGDGDHDYDUDUDUDHDHDHSHAHAHSHSBDHDHDHDHDHD
srlegaspi2101
 
PDF
Web 11 | AJAX + JSON + PHP
Mohammad Imam Hossain
 
PPT
Ajax introduction
sjmittal
 
PPT
Mashup
Naveen P.N
 
Web technologies-course 10.pptx
Stefan Oprea
 
Introduction to AJAX
Abzetdin Adamov
 
AJAX and JSON
Julie Iskander
 
Web-Engineering-Lec-14 (1) .pptx
iamayesha2526
 
Web-Engineering-Lec-14 (1 ).pptx
iamayesha2526
 
Web Programming using Asynchronous JavaX
SivanN6
 
AJAX - An introduction
Eleonora Ciceri
 
WEB TECHNOLOGY Unit-5.pptx
karthiksmart21
 
Ajax
Tech_MX
 
How to make Ajax work for you
Simon Willison
 
Introduction to Ajax programming
Fulvio Corno
 
AJAX.pptx
Ganesh Chavan
 
Unit-5.pptx
itzkuu01
 
Ajax
soumya
 
HSHDGDGDHDYDUDUDUDHDHDHSHAHAHSHSBDHDHDHDHDHD
srlegaspi2101
 
Web 11 | AJAX + JSON + PHP
Mohammad Imam Hossain
 
Ajax introduction
sjmittal
 
Mashup
Naveen P.N
 
Ad

More from Seble Nigussie (9)

PDF
Fundamentals of programming with C++
Seble Nigussie
 
PDF
Introduction to jQuery
Seble Nigussie
 
PDF
Introduction to Javascript
Seble Nigussie
 
PDF
Introduction to Bootstrap
Seble Nigussie
 
PDF
Flexbox, Grid and Sass
Seble Nigussie
 
PDF
Introduction to CSS3
Seble Nigussie
 
PDF
Introduction to HTML
Seble Nigussie
 
PDF
Introduction to HTTP
Seble Nigussie
 
PDF
Introduction to Microprocessors
Seble Nigussie
 
Fundamentals of programming with C++
Seble Nigussie
 
Introduction to jQuery
Seble Nigussie
 
Introduction to Javascript
Seble Nigussie
 
Introduction to Bootstrap
Seble Nigussie
 
Flexbox, Grid and Sass
Seble Nigussie
 
Introduction to CSS3
Seble Nigussie
 
Introduction to HTML
Seble Nigussie
 
Introduction to HTTP
Seble Nigussie
 
Introduction to Microprocessors
Seble Nigussie
 
Ad

Recently uploaded (20)

PPTX
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
PPTX
INDUCTIVE EFFECT slide for first prof pharamacy students
SHABNAM FAIZ
 
PDF
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
PPTX
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
 
PDF
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
 
PDF
Hurricane Helene Application Documents Checklists
Mebane Rash
 
PPTX
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
PPTX
YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
PPTX
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
PPTX
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
PPTX
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
PPTX
Photo chemistry Power Point Presentation
mprpgcwa2024
 
PPTX
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
PDF
LDMMIA Shop & Student News Summer Solstice 25
LDM & Mia eStudios
 
PPTX
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
PPTX
Birnagar High School Platinum Jubilee Quiz.pptx
Sourav Kr Podder
 
PPTX
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
PPTX
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
PPTX
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
PPTX
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
INDUCTIVE EFFECT slide for first prof pharamacy students
SHABNAM FAIZ
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
 
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
 
Hurricane Helene Application Documents Checklists
Mebane Rash
 
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
Photo chemistry Power Point Presentation
mprpgcwa2024
 
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
LDMMIA Shop & Student News Summer Solstice 25
LDM & Mia eStudios
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
Birnagar High School Platinum Jubilee Quiz.pptx
Sourav Kr Podder
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 

Introduction to JSON & Ajax

  • 2. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 2 • JSON is a syntax for storing and exchanging data • JSON is an easier-to-use alternative to XML • JSON is language independent • JSON uses JavaScript syntax, but the JSON format is text only, just like XML
  • 3. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 3 JSON XML
  • 4. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 4 Similarities • Both JSON and XML are hierarchical (values within values) • Both JSON and XML can be parsed by any programming language • Both JSON and XML can be fetched with an XMLHttpRequest Differences • JSON doesn't use end tag • JSON is shorter • JSON is quicker to read and write • JSON can use arrays • XML has to be parsed with an XML parser. JSON can be parsed by a standard JavaScript function
  • 5. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 5 • JSON syntax is derived from JavaScript object notation • Example: • Javascript Object • JSON Object • JSON Text/String • JSON Array
  • 6. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 6 • Data is in name/value pairs • name and value are separated by “:” and field name is double quoted • Value can be • A number (integer or floating point) • A string (in double quotes) • A Boolean (true or false) • An array (in square brackets) • An object (in curly braces) • null
  • 7. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 7 • The JavaScript function JSON.parse(text) can be used to convert a JSON text into a JavaScript object: • The JavaScript function JSON.stringify(object) can be used to convert JavaScript object to JSON String
  • 8. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 8 • A common use of JSON is to exchange data between the client and a web server • Objects in PHP can be converted into JSON by using the PHP function json_encode()
  • 9. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 9 Asynchronous JavaScript and XML • AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes • With Ajax you can:- • Update a web page without reloading the page • Request data from a server - after the page has loaded • Receive data from a server - after the page has loaded • Send data to a server - in the background • Examples of applications using AJAX: • Google Maps, Gmail, YouTube, and Facebook.
  • 10. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 10
  • 11. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 11 • Many of the tasks performed on the server are very time consuming. Before AJAX, retrieving data from server behind the scenes could cause the application to hang or stop • With AJAX, the JavaScript does not have to wait for the server response, but can instead: • execute other scripts while waiting for server response • deal with the response when the response ready
  • 12. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 12 XMLHttpRequest Object • The keystone of AJAX is the XMLHttpRequest object • The XMLHttpRequest object is used to exchange data with a server behind the scenes • Syntax for creating an XMLHttpRequest object: • var xhttp = new XMLHttpRequest();
  • 13. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 13 Send a Request to a Server • To send a request to a server, the open() and send() methods of the XMLHttpRequest object are used: Method Description open(method, url, async) Specifies the type of request method: the type of request: GET or POST url: the server (file) location async: true (asynchronous) or false (synchronous) send() Sends the request to the server (used for GET) send(string) Sends the request to the server (used for POST)
  • 14. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 14 Send a Request to a Server … • xhttp.open("GET", "ajax_info.txt", true); xhttp.send(); • xhttp.open("GET", “reg.php?fname=Henry&lname=Ford", true); xhttp.send(); • xhttp.open("POST", "demo_post.php", true); xhttp.send(); • To POST form, add an HTTP header with setRequestHeader() xhttp.open("POST", "ajax_test.asp", true); xhttp.setRequestHeader("Content-type", "application/x-www-form- urlencoded"); xhttp.send("fname=Henry&lname=Ford");
  • 15. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 15 AJAX - Server Response • The response from a server, can be accessed through the responseText or responseXML property of the XMLHttpRequest object • responseText - get the response data as a string • responseXML - get the response data as XML data • document.getElementById("demo").innerHTML = xhttp.responseText;
  • 16. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 16 • The readyState property holds the status of the XMLHttpRequest: Changes from 0 to 4 • 0: request not initialized 1: server connection established 2: request received 3: processing request 4: request finished and response is ready • The onreadystatechange event is triggered every time the readyState changes.
  • 17. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 17 • onreadystatechange event: specify what will happen when the server response is ready to be processed • When readyState is 4 and status is 200, the response is ready
  • 18. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 18 • If you have more than one AJAX task on your website, you should create ONE standard function for creating the XMLHttpRequest object, and call this for each AJAX task
  • 19. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 19 • jQuery provides several methods for AJAX functionality. • With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post
  • 20. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 20 • The load() method loads data from a server and puts the returned data into the selected element • Syntax • $(selector).load(URL,data,callback); • URL: required parameter, specifies the URL you wish to load. • data: optional, specifies a set of querystring key/value pairs to send along with the request. • callback: optional, is the name of a function to be executed after the load() method is completed. • $("#div1").load("demo_test.txt");
  • 21. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 21 • specifies a callback function to run when the load() method is completed • The callback function can have different parameters: • responseTxt - contains the resulting content if the call succeeds • statusTxt - contains the status of the call • xhr - contains the XMLHttpRequest object
  • 22. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 22 • The jQuery get() and post() methods are used to request data from the server with an HTTP GET or POST request • GET is used for just getting data from the server • Note: The GET method may return cached data • POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.
  • 23. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 23 • The $.get() method requests data from the server with an HTTP GET request. • Syntax • $.get(URL,callback); • URL : required parameter, specifies the URL you wish to request. • callback: optional parameter , is the name of a function to be executed if the request succeeds. Has two parameters: • Data: holds the content of the page requested • Status: holds the status of the request
  • 24. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 24
  • 25. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 25 • The $.post() method requests data from the server with an HTTP POST request. • Syntax • $.get(URL,data, callback); • URL : required parameter, specifies the URL you wish to request. • data: optional, specifies some data to send along with the request • callback: optional parameter , is the name of a function to be executed if the request succeeds. Has two parameters: • Data: holds the content of the page requested • Status: holds the status of the request
  • 26. C O M P I L E D B Y : S E B L E N . D E P A R T M E N T O F C O M P U T E R S C . A D D I S A B A B A U N I V E R S I T Y 26