SlideShare a Scribd company logo
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

More Related Content

What's hot

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

What's hot (20)

Indexing and Performance Tuning
Indexing and Performance TuningIndexing and Performance Tuning
Indexing and Performance Tuning
 
The Lonesome LOD Cloud
The Lonesome LOD CloudThe Lonesome LOD Cloud
The Lonesome LOD Cloud
 
Jsonsaga 100605143125-phpapp02
Jsonsaga 100605143125-phpapp02Jsonsaga 100605143125-phpapp02
Jsonsaga 100605143125-phpapp02
 
Json the-x-in-ajax1588
Json the-x-in-ajax1588Json the-x-in-ajax1588
Json the-x-in-ajax1588
 
Web data from R
Web data from RWeb data from R
Web data from R
 
Concise at NTU Graduate Institute of Linguistics
Concise at NTU Graduate Institute of Linguistics Concise at NTU Graduate Institute of Linguistics
Concise at NTU Graduate Institute of Linguistics
 
Xcap tutorial
Xcap tutorialXcap tutorial
Xcap tutorial
 
Introduction to solr
Introduction to solrIntroduction to solr
Introduction to solr
 
Linked Data Fragments
Linked Data FragmentsLinked Data Fragments
Linked Data Fragments
 
The Aggregation Framework
The Aggregation FrameworkThe Aggregation Framework
The Aggregation Framework
 
よく使うテストヘルパーの紹介 #ios_test_night
よく使うテストヘルパーの紹介 #ios_test_nightよく使うテストヘルパーの紹介 #ios_test_night
よく使うテストヘルパーの紹介 #ios_test_night
 
useR! 2012 Talk
useR! 2012 TalkuseR! 2012 Talk
useR! 2012 Talk
 
MongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
MongoDB Europe 2016 - Advanced MongoDB Aggregation PipelinesMongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
MongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
 
MongoDB Europe 2016 - Graph Operations with MongoDB
MongoDB Europe 2016 - Graph Operations with MongoDBMongoDB Europe 2016 - Graph Operations with MongoDB
MongoDB Europe 2016 - Graph Operations with MongoDB
 
Analytics with MongoDB Aggregation Framework and Hadoop Connector
Analytics with MongoDB Aggregation Framework and Hadoop ConnectorAnalytics with MongoDB Aggregation Framework and Hadoop Connector
Analytics with MongoDB Aggregation Framework and Hadoop Connector
 
Introduction to couch_db
Introduction to couch_dbIntroduction to couch_db
Introduction to couch_db
 
Getting started with apache solr
Getting started with apache solrGetting started with apache solr
Getting started with apache solr
 
Data Processing and Aggregation with MongoDB
Data Processing and Aggregation with MongoDB Data Processing and Aggregation with MongoDB
Data Processing and Aggregation with MongoDB
 
Json at work overview and ecosystem-v2.0
Json at work   overview and ecosystem-v2.0Json at work   overview and ecosystem-v2.0
Json at work overview and ecosystem-v2.0
 
Querying the Web of Data
Querying the Web of DataQuerying the Web of Data
Querying the Web of Data
 

Similar to Introduction to JSON & Ajax

Web technologies-course 10.pptx
Web technologies-course 10.pptxWeb technologies-course 10.pptx
Web technologies-course 10.pptx
Stefan Oprea
 
Consuming RESTful Web services in PHP
Consuming RESTful Web services in PHPConsuming RESTful Web services in PHP
Consuming RESTful Web services in PHP
Zoran Jeremic
 
Consuming RESTful services in PHP
Consuming RESTful services in PHPConsuming RESTful services in PHP
Consuming RESTful services in PHP
Zoran Jeremic
 
Web Service and Mobile Integrated Day I
Web Service and Mobile Integrated Day IWeb Service and Mobile Integrated Day I
Web Service and Mobile Integrated Day I
Anuchit Chalothorn
 
Java script
Java scriptJava script
Java script
vishal choudhary
 
JSON
JSONJSON
JSON
Yoga Raja
 
Ajaxtechnicalworkshopshortversion 1224845432835700-8
Ajaxtechnicalworkshopshortversion 1224845432835700-8Ajaxtechnicalworkshopshortversion 1224845432835700-8
Ajaxtechnicalworkshopshortversion 1224845432835700-8
anuradha raheja
 
Asynchronous single page applications without a line of HTML or Javascript, o...
Asynchronous single page applications without a line of HTML or Javascript, o...Asynchronous single page applications without a line of HTML or Javascript, o...
Asynchronous single page applications without a line of HTML or Javascript, o...
Robert Schadek
 
AJAX.pptx
AJAX.pptxAJAX.pptx
AJAX.pptx
Ganesh Chavan
 
Web Scraping_ Gathering Data from Websites.pptx
Web Scraping_ Gathering Data from Websites.pptxWeb Scraping_ Gathering Data from Websites.pptx
Web Scraping_ Gathering Data from Websites.pptx
HitechIOT
 
Meteor - not just for rockstars
Meteor - not just for rockstarsMeteor - not just for rockstars
Meteor - not just for rockstars
Stephan Hochhaus
 
AJAX.pptx
AJAX.pptxAJAX.pptx
AJAX.pptx
ssuser0a07a1
 
Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7
Pranav Prakash
 
The Key Features of a Great Web API
The Key Features of a Great Web APIThe Key Features of a Great Web API
The Key Features of a Great Web API
Nordic APIs
 
TDC2016SP - Trilha Frameworks JavaScript
TDC2016SP - Trilha Frameworks JavaScriptTDC2016SP - Trilha Frameworks JavaScript
TDC2016SP - Trilha Frameworks JavaScript
tdc-globalcode
 
React for Beginners
React for BeginnersReact for Beginners
React for Beginners
Derek Willian Stavis
 
Web services tutorial
Web services tutorialWeb services tutorial
Web services tutorial
Lorna Mitchell
 
Web Services Tutorial
Web Services TutorialWeb Services Tutorial
Web Services Tutorial
Lorna Mitchell
 
Web Services PHP Tutorial
Web Services PHP TutorialWeb Services PHP Tutorial
Web Services PHP Tutorial
Lorna Mitchell
 
Austin Day of Rest - Introduction
Austin Day of Rest - IntroductionAustin Day of Rest - Introduction
Austin Day of Rest - Introduction
HandsOnWP.com
 

Similar to Introduction to JSON & Ajax (20)

Web technologies-course 10.pptx
Web technologies-course 10.pptxWeb technologies-course 10.pptx
Web technologies-course 10.pptx
 
Consuming RESTful Web services in PHP
Consuming RESTful Web services in PHPConsuming RESTful Web services in PHP
Consuming RESTful Web services in PHP
 
Consuming RESTful services in PHP
Consuming RESTful services in PHPConsuming RESTful services in PHP
Consuming RESTful services in PHP
 
Web Service and Mobile Integrated Day I
Web Service and Mobile Integrated Day IWeb Service and Mobile Integrated Day I
Web Service and Mobile Integrated Day I
 
Java script
Java scriptJava script
Java script
 
JSON
JSONJSON
JSON
 
Ajaxtechnicalworkshopshortversion 1224845432835700-8
Ajaxtechnicalworkshopshortversion 1224845432835700-8Ajaxtechnicalworkshopshortversion 1224845432835700-8
Ajaxtechnicalworkshopshortversion 1224845432835700-8
 
Asynchronous single page applications without a line of HTML or Javascript, o...
Asynchronous single page applications without a line of HTML or Javascript, o...Asynchronous single page applications without a line of HTML or Javascript, o...
Asynchronous single page applications without a line of HTML or Javascript, o...
 
AJAX.pptx
AJAX.pptxAJAX.pptx
AJAX.pptx
 
Web Scraping_ Gathering Data from Websites.pptx
Web Scraping_ Gathering Data from Websites.pptxWeb Scraping_ Gathering Data from Websites.pptx
Web Scraping_ Gathering Data from Websites.pptx
 
Meteor - not just for rockstars
Meteor - not just for rockstarsMeteor - not just for rockstars
Meteor - not just for rockstars
 
AJAX.pptx
AJAX.pptxAJAX.pptx
AJAX.pptx
 
Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7
 
The Key Features of a Great Web API
The Key Features of a Great Web APIThe Key Features of a Great Web API
The Key Features of a Great Web API
 
TDC2016SP - Trilha Frameworks JavaScript
TDC2016SP - Trilha Frameworks JavaScriptTDC2016SP - Trilha Frameworks JavaScript
TDC2016SP - Trilha Frameworks JavaScript
 
React for Beginners
React for BeginnersReact for Beginners
React for Beginners
 
Web services tutorial
Web services tutorialWeb services tutorial
Web services tutorial
 
Web Services Tutorial
Web Services TutorialWeb Services Tutorial
Web Services Tutorial
 
Web Services PHP Tutorial
Web Services PHP TutorialWeb Services PHP Tutorial
Web Services PHP Tutorial
 
Austin Day of Rest - Introduction
Austin Day of Rest - IntroductionAustin Day of Rest - Introduction
Austin Day of Rest - Introduction
 

More from Seble Nigussie

Fundamentals of programming with C++
Fundamentals of programming with C++Fundamentals of programming with C++
Fundamentals of programming with C++
Seble Nigussie
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
Seble Nigussie
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
Seble Nigussie
 
Introduction to Bootstrap
Introduction to BootstrapIntroduction to Bootstrap
Introduction to Bootstrap
Seble Nigussie
 
Flexbox, Grid and Sass
Flexbox, Grid and SassFlexbox, Grid and Sass
Flexbox, Grid and Sass
Seble Nigussie
 
Introduction to CSS3
Introduction to CSS3Introduction to CSS3
Introduction to CSS3
Seble Nigussie
 
Introduction to HTML
Introduction to HTMLIntroduction to HTML
Introduction to HTML
Seble Nigussie
 
Introduction to HTTP
Introduction to HTTPIntroduction to HTTP
Introduction to HTTP
Seble Nigussie
 
Introduction to Microprocessors
Introduction to MicroprocessorsIntroduction to Microprocessors
Introduction to Microprocessors
Seble Nigussie
 

More from Seble Nigussie (9)

Fundamentals of programming with C++
Fundamentals of programming with C++Fundamentals of programming with C++
Fundamentals of programming with C++
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
Introduction to Bootstrap
Introduction to BootstrapIntroduction to Bootstrap
Introduction to Bootstrap
 
Flexbox, Grid and Sass
Flexbox, Grid and SassFlexbox, Grid and Sass
Flexbox, Grid and Sass
 
Introduction to CSS3
Introduction to CSS3Introduction to CSS3
Introduction to CSS3
 
Introduction to HTML
Introduction to HTMLIntroduction to HTML
Introduction to HTML
 
Introduction to HTTP
Introduction to HTTPIntroduction to HTTP
Introduction to HTTP
 
Introduction to Microprocessors
Introduction to MicroprocessorsIntroduction to Microprocessors
Introduction to Microprocessors
 

Recently uploaded

The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 

Recently uploaded (20)

The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 

Introduction to JSON & Ajax

  • 1.
  • 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