SlideShare una empresa de Scribd logo
1 de 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
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

Más contenido relacionado

La actualidad más candente

Indexing and Performance Tuning
Indexing and Performance TuningIndexing and Performance Tuning
Indexing and Performance TuningMongoDB
 
The Lonesome LOD Cloud
The Lonesome LOD CloudThe Lonesome LOD Cloud
The Lonesome LOD CloudRuben Verborgh
 
Jsonsaga 100605143125-phpapp02
Jsonsaga 100605143125-phpapp02Jsonsaga 100605143125-phpapp02
Jsonsaga 100605143125-phpapp02Ramamohan Chokkam
 
Web data from R
Web data from RWeb data from R
Web data from Rschamber
 
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 tutorialwanglixue
 
The Aggregation Framework
The Aggregation FrameworkThe Aggregation Framework
The Aggregation FrameworkMongoDB
 
よく使うテストヘルパーの紹介 #ios_test_night
よく使うテストヘルパーの紹介 #ios_test_nightよく使うテストヘルパーの紹介 #ios_test_night
よく使うテストヘルパーの紹介 #ios_test_nightKenji Tanaka
 
useR! 2012 Talk
useR! 2012 TalkuseR! 2012 Talk
useR! 2012 Talkrtelmore
 
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 PipelinesMongoDB
 
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 MongoDBMongoDB
 
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 ConnectorHenrik Ingo
 
Introduction to couch_db
Introduction to couch_dbIntroduction to couch_db
Introduction to couch_dbRomain Testard
 
Getting started with apache solr
Getting started with apache solrGetting started with apache solr
Getting started with apache solrHumayun 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
 
Querying the Web of Data
Querying the Web of DataQuerying the Web of Data
Querying the Web of DataRinke Hoekstra
 

La actualidad más candente (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 a Introduction to JSON & Ajax

Web technologies-course 10.pptx
Web technologies-course 10.pptxWeb technologies-course 10.pptx
Web technologies-course 10.pptxStefan Oprea
 
Consuming RESTful Web services in PHP
Consuming RESTful Web services in PHPConsuming RESTful Web services in PHP
Consuming RESTful Web services in PHPZoran Jeremic
 
Consuming RESTful services in PHP
Consuming RESTful services in PHPConsuming RESTful services in PHP
Consuming RESTful services in PHPZoran 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 IAnuchit Chalothorn
 
Ajaxtechnicalworkshopshortversion 1224845432835700-8
Ajaxtechnicalworkshopshortversion 1224845432835700-8Ajaxtechnicalworkshopshortversion 1224845432835700-8
Ajaxtechnicalworkshopshortversion 1224845432835700-8anuradha 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
 
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.pptxHitechIOT
 
Meteor - not just for rockstars
Meteor - not just for rockstarsMeteor - not just for rockstars
Meteor - not just for rockstarsStephan Hochhaus
 
Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7Pranav 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 APINordic APIs
 
TDC2016SP - Trilha Frameworks JavaScript
TDC2016SP - Trilha Frameworks JavaScriptTDC2016SP - Trilha Frameworks JavaScript
TDC2016SP - Trilha Frameworks JavaScripttdc-globalcode
 
Web Services PHP Tutorial
Web Services PHP TutorialWeb Services PHP Tutorial
Web Services PHP TutorialLorna Mitchell
 
Austin Day of Rest - Introduction
Austin Day of Rest - IntroductionAustin Day of Rest - Introduction
Austin Day of Rest - IntroductionHandsOnWP.com
 

Similar a 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
 

Más de 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 jQuerySeble Nigussie
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to JavascriptSeble Nigussie
 
Introduction to Bootstrap
Introduction to BootstrapIntroduction to Bootstrap
Introduction to BootstrapSeble Nigussie
 
Flexbox, Grid and Sass
Flexbox, Grid and SassFlexbox, Grid and Sass
Flexbox, Grid and SassSeble Nigussie
 
Introduction to Microprocessors
Introduction to MicroprocessorsIntroduction to Microprocessors
Introduction to MicroprocessorsSeble Nigussie
 

Más de 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
 

Último

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 

Último (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 

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