SlideShare a Scribd company logo
COURSE 10
JAVASCRIPT
lect. eng. Rajmond JÁNÓ, PhD
rajmond.jano@ael.utcl
uj.ro
fb.com/janorajmond
C10 – JAVASCRIPT
• JSON
• AJAX
JSON
• JSON stands for JavaScript Object Notation
• JSON is a syntax for storing and exchanging data
• JSON is text, written with JavaScript object notation
• JSON is "self-describing" and easy to understand
• JSON is language independent
• JSON uses JavaScript syntax, but the JSON format is text
only
• Text can be read and used as a data format by any
programming language
JSON SYNTAX
JSON syntax is derived from JavaScript object
notation syntax:
• Data is in name/value pairs
• Data is separated by commas
• Curly braces hold objects
• Square brackets hold arrays
JSON DATA
• JSON data is written as name/value pairs
• A name/value pair consists of a field name (in
double quotes), followed by a colon, followed
by a value:
JSON JavaScript
JSON always uses double quotes
JSON FILES
• The file type for JSON files is ".json"
JSON VS XML
• Both JSON and XML can be used to receive data
from a web server
JSON
XML
JSON VS XML
• XML is much more difficult to parse than JSON
• JSON is parsed into a ready-to-use JavaScript object
• For AJAX applications, JSON is faster and easier than
XML
• Using XML
• Fetch an XML document
• Use the XML DOM to loop through the document
• Extract values and store in variables
• Using JSON
• Fetch a JSON string
• JSON.Parse the JSON string
JSON DATA TYPES
In JSON, values must be one of the following data
types:
• a string
• a number
• an object (JSON object)
• an array
• a boolean
• null
JSON values cannot be one of the following data
types:
• a function
• a date
JSON OBJECTS
• JSON objects are surrounded by curly braces {}
• JSON objects are written in key/value pairs
• Keys must be strings, and values must be a
valid JSON data type (string, number, object,
array, boolean or null)
• Keys and values are separated by a colon
• Each key/value pair is separated by a comma
NESTED JSON OBJECTS
• Values in a JSON object can be another JSON
object
ACCESSING OBJECT VALUES
• You can access the object values by using dot
(.) notation
LOOPING THROUGH AN OBJECT
• You can loop through object properties by
using the for-in loop
LOOPING THROUGH AN OBJECT
• In a for-in loop, use the bracket notation to
access the property values
UPDATING AND DELETING
VALUES
• You can use the dot notation
to modify any value in a JSON
object
• You can also use the bracket
notation to modify a value in
a JSON object
• Use the delete keyword to
delete properties from a JSON
object
JSON PARSE
• A common use of JSON is to exchange data
to/from a web server
• When receiving data from a web server, the
data is always a string
• Parse the data with JSON.parse(), and the
data becomes a JavaScript object
JSON PARSE
JSON STRINGIFY
• When sending data to a web server, the data
has to be a string
• Convert a JavaScript object into a string with
JSON.stringify()
AJAX
AJAX is used to
• Read data from a web server - after the page has
loaded
• Update a web page without reloading the page
• Send data to a web server - in the background
AJAX
• AJAX stands for Asynchronous JavaScript And XML
• AJAX is not a programming language
• AJAX just uses a combination of:
• A browser built-in XMLHttpRequest object (to request data from a
web server)
• JavaScript and HTML DOM (to display or use the data)
• AJAX is a misleading name
• AJAX applications might use XML to transport data, but it is more
common to transport data as plain text or JSON text
• AJAX allows web pages to be updated asynchronously by
exchanging data with a web server behind the scenes
• This means that it is possible to update parts of a web page,
without reloading the whole page
AJAX
Data request
An event occurs:
• Create an XMLHttpRequest
object
• Send HttpRequest
Backend
• Process HttpRequest
• Create a response and send
back to the browser
Data processing
• Process the returned data
using JavaScript
• Update page content
Interne
t
Interne
t
Browser
Server
THE XMLHTTPREQUEST OBJECT
• The keystone of AJAX is the XMLHttpRequest
object
• All modern browsers support the
XMLHttpRequest object
• The XMLHttpRequest object can be used to
exchange data with a web server behind the
scenes
• This means that it is possible to update parts of a
web page, without reloading the whole page
THE XMLHTTPREQUEST OBJECT
• Syntax for creating an XMLHttpRequest object
• For security reasons, modern browsers do not
allow access across domains
• This means that both the web page and the XML file
it tries to load, must be located on the same server
XMLHTTPREQUEST OBJECT
METHODS
Method Description
new XMLHttpRequest() Creates a new XMLHttpRequest object
abort() Cancels the current request
getAllResponseHeaders() Returns header information
getResponseHeader() Returns specific header information
open(method, url, async,
user, psw)
Specifies the request
method: the request type GET or POST
url: the file location
async: true (asynchronous) or false (synchronous)
user: optional username
psw: optional password
send()
Sends the request to the server
Used for GET requests
send(string)
Sends the request to the server.
Used for POST requests
setRequestHeader()
Adds a label/value pair to the header to
XMLHTTPREQUEST OBJECT
PROPERTIES
Property Description
onreadystatechange
Defines a function to be called when the
readyState property changes
readyState
Holds the status of the XMLHttpRequest
0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready
responseText Returns the response data as a string
responseXML Returns the response data as XML data
status
Returns the status-number of a request
200: "OK"
403: "Forbidden"
404: "Not Found“
… etc.
statusText
Returns the status-text (e.g. "OK" or "Not
AJAX – GETTING DATA FROM A
SERVER
• To send a request to a server, we use the
open() and send() methods of the
XMLHttpRequest object
• We then used the GET method, to download
data from the server
AJAX – SENDING DATA TO A SERVER
• To send a request to a server, we use the
open() and send() methods of the
XMLHttpRequest object
• We then used the POST method, to upload data
to the server
AJAX – SERVER RESPONSE
• The onreadystatechange property
• The readyState property holds the status of the
XMLHttpRequest
• The onreadystatechange property defines a function
to be executed when the readyState changes
• The status property and the statusText property
holds the status of the XMLHttpRequest object
• The onreadystatechange function is called every
time the readyState changes
AJAX – SERVER RESPONSE
• Using a Callback Function
• A callback function is a function passed as a
parameter to another function
• If you have more than one AJAX task in a website,
you should create one function for executing the
XMLHttpRequest object, and one callback function
for each AJAX task
• The function call should contain the URL and what
function to call when the response is ready
AJAX – SERVER RESPONSE
AJAX – SERVER RESPONSE
• Alternatively, the onload function can be used
• The onload function is called every time data
is available from the server
JSON & AJAX – EXAMPLE 01
• Design a webpage that on every button press
loads 3 pieces of data
JSON & AJAX – EXAMPLE 01
• To get data, we are going to use the “Users”
section of the
https://jsonplaceholder.typicode.com page
• To get all the comments we can access
https://jsonplaceholder.typicode.com/users/
JSON & AJAX – EXAMPLE 01
JSON & AJAX – EXAMPLE 01
JSON & AJAX – EXAMPLE 01
JSON & AJAX – EXAMPLE 01
JSON & AJAX – EXAMPLE 01
JSON & AJAX – EXAMPLE 01
Why?
JSON & AJAX – EXAMPLE 01
JSON & AJAX – EXAMPLE 01
JSON & AJAX – EXAMPLE 01
Why?
JSON & AJAX – EXAMPLE 01
JSON & AJAX – EXAMPLE 01
JSON & AJAX – EXAMPLE 01
JSON & AJAX – EXAMPLE 01
JSON & AJAX – EXAMPLE 02
• Design a webpage with infinite scroll
• Content is dynamically loaded whenever the user
gets close to the bottom of the page (e.g.: Facebook,
9GAG, etc.)
JSON & AJAX – EXAMPLE 02
• To get data, we are going to use the
“Comments” section of the
https://jsonplaceholder.typicode.com page
• To get all the comments we can access
https://jsonplaceholder.typicode.com/comme
nts/
JSON & AJAX – EXAMPLE 02
JSON & AJAX – EXAMPLE 02
• To get a single comment, we can access
https://jsonplaceholder.typicode.com/comme
nts/id
JSON & AJAX – EXAMPLE 02
• Most HTML will be dynamically generated, so
our starting index.html file is very simple
JSON & AJAX – EXAMPLE 02
• Now, we must design how we want a comment
to look like on our webpage
h1 with id of
“name”
h3 with id of
“email”
h1 with id of
“label”
p with id of
“message”
scroll position
counters
(for debugging only)
JSON & AJAX – EXAMPLE 02
JSON & AJAX – EXAMPLE 02
• First, we want to cache the DOM
• Then we want to load at least some data onto
our webpage: we’ll load initially 10 pieces of
data
JSON & AJAX – EXAMPLE 02
• The renderComment(cnt) function gets the
data from the server and generates the HTML
from it
JSON & AJAX – EXAMPLE 02
• The renderComment(cnt) function gets the
data from the server and generates the HTML
from it
JSON & AJAX – EXAMPLE 02
Why isn’t my
data in the
correct order?
JSON & AJAX – EXAMPLE 02
• An AJAX request is asynchronous by default
• It does not wait for the server response before
executing the next line of code → data that arrives
earlier will be the first to be displayed
• Can this be fixed?
• Yes, we can make the call synchronous
JSON & AJAX – EXAMPLE 02
JSON & AJAX – EXAMPLE 02
• In order to load new data when the user
reaches the end of the page, we must
determine when he reached the end of the
page and take action (i.e.: load new data)
• In order to achieve this, we “listen” every time
the user scrolls:
• Determine where the current scroll position is
• Determine what is the maximum scrollable amount
• If the current scroll positions is the maximum
scrollable amount, then we know we must load new
JSON & AJAX – EXAMPLE 02
JSON & AJAX – EXAMPLE 02
JSON & AJAX – EXAMPLE 02
JSON & AJAX – EXAMPLE 02
JSON & AJAX – EXAMPLE 02
• Identify possible code failure points
BIBLIOGRAPHY
• https://www.w3schools.com/js/js_json_intro.a
sp
• https://www.w3schools.com/js/js_ajax_intro.a
sp
• https://www.w3schools.com/js/js_window.asp
• https://jsonplaceholder.typicode.com
• http://www.filltext.com/
COURSES
Available online at:
http://www.ael.utcluj.ro/
Information for Students -> Courses -> Web
Technologies
Web technologies-course 10.pptx

More Related Content

Similar to Web technologies-course 10.pptx

Ajax Technology
Ajax TechnologyAjax Technology
Ajax Technology
Zia_Rehman
 
Ajaxtechnicalworkshopshortversion 1224845432835700-8
Ajaxtechnicalworkshopshortversion 1224845432835700-8Ajaxtechnicalworkshopshortversion 1224845432835700-8
Ajaxtechnicalworkshopshortversion 1224845432835700-8
anuradha raheja
 
Web Development Course - AJAX & JSON by RSOLUTIONS
Web Development Course - AJAX & JSON by RSOLUTIONSWeb Development Course - AJAX & JSON by RSOLUTIONS
Web Development Course - AJAX & JSON by RSOLUTIONS
RSolutions
 
PHP - Introduction to PHP AJAX
PHP -  Introduction to PHP AJAXPHP -  Introduction to PHP AJAX
PHP - Introduction to PHP AJAX
Vibrant Technologies & Computers
 
Ajax
AjaxAjax
WEB TECHNOLOGY Unit-5.pptx
WEB TECHNOLOGY Unit-5.pptxWEB TECHNOLOGY Unit-5.pptx
WEB TECHNOLOGY Unit-5.pptx
karthiksmart21
 
Learn AJAX at ASIT
Learn AJAX at ASITLearn AJAX at ASIT
Learn AJAX at ASIT
ASIT
 
Introduction to JSON & Ajax
Introduction to JSON & AjaxIntroduction to JSON & Ajax
Introduction to JSON & Ajax
Seble Nigussie
 
Json - ideal for data interchange
Json - ideal for data interchangeJson - ideal for data interchange
Json - ideal for data interchange
Christoph Santschi
 
Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7
Pranav Prakash
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
Nir Elbaz
 
Ajax tutorial by bally chohan
Ajax tutorial by bally chohanAjax tutorial by bally chohan
Ajax tutorial by bally chohan
WebVineet
 
Con fess 2013-sse-websockets-json-bhakti
Con fess 2013-sse-websockets-json-bhaktiCon fess 2013-sse-websockets-json-bhakti
Con fess 2013-sse-websockets-json-bhaktiBhakti Mehta
 
AJAX.pptx
AJAX.pptxAJAX.pptx
AJAX.pptx
ssuser0a07a1
 
json.ppt download for free for college project
json.ppt download for free for college projectjson.ppt download for free for college project
json.ppt download for free for college project
AmitSharma397241
 

Similar to Web technologies-course 10.pptx (20)

Ajax Technology
Ajax TechnologyAjax Technology
Ajax Technology
 
Ajaxtechnicalworkshopshortversion 1224845432835700-8
Ajaxtechnicalworkshopshortversion 1224845432835700-8Ajaxtechnicalworkshopshortversion 1224845432835700-8
Ajaxtechnicalworkshopshortversion 1224845432835700-8
 
Web Development Course - AJAX & JSON by RSOLUTIONS
Web Development Course - AJAX & JSON by RSOLUTIONSWeb Development Course - AJAX & JSON by RSOLUTIONS
Web Development Course - AJAX & JSON by RSOLUTIONS
 
PHP - Introduction to PHP AJAX
PHP -  Introduction to PHP AJAXPHP -  Introduction to PHP AJAX
PHP - Introduction to PHP AJAX
 
Ajax
AjaxAjax
Ajax
 
WEB TECHNOLOGY Unit-5.pptx
WEB TECHNOLOGY Unit-5.pptxWEB TECHNOLOGY Unit-5.pptx
WEB TECHNOLOGY Unit-5.pptx
 
Ajax
AjaxAjax
Ajax
 
Ajax
Ajax Ajax
Ajax
 
Learn AJAX at ASIT
Learn AJAX at ASITLearn AJAX at ASIT
Learn AJAX at ASIT
 
Introduction to JSON & Ajax
Introduction to JSON & AjaxIntroduction to JSON & Ajax
Introduction to JSON & Ajax
 
Json - ideal for data interchange
Json - ideal for data interchangeJson - ideal for data interchange
Json - ideal for data interchange
 
Ajax Tuturial
Ajax TuturialAjax Tuturial
Ajax Tuturial
 
Ajax
AjaxAjax
Ajax
 
Ajax
AjaxAjax
Ajax
 
Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
 
Ajax tutorial by bally chohan
Ajax tutorial by bally chohanAjax tutorial by bally chohan
Ajax tutorial by bally chohan
 
Con fess 2013-sse-websockets-json-bhakti
Con fess 2013-sse-websockets-json-bhaktiCon fess 2013-sse-websockets-json-bhakti
Con fess 2013-sse-websockets-json-bhakti
 
AJAX.pptx
AJAX.pptxAJAX.pptx
AJAX.pptx
 
json.ppt download for free for college project
json.ppt download for free for college projectjson.ppt download for free for college project
json.ppt download for free for college project
 

More from Stefan Oprea

Training-Book-Samsung.ppt
Training-Book-Samsung.pptTraining-Book-Samsung.ppt
Training-Book-Samsung.ppt
Stefan Oprea
 
PRESENTATION ON TOUCH TECHNOLOGY.ppt
PRESENTATION ON TOUCH TECHNOLOGY.pptPRESENTATION ON TOUCH TECHNOLOGY.ppt
PRESENTATION ON TOUCH TECHNOLOGY.ppt
Stefan Oprea
 
Web technologies-course 12.pptx
Web technologies-course 12.pptxWeb technologies-course 12.pptx
Web technologies-course 12.pptx
Stefan Oprea
 
Web technologies-course 11.pptx
Web technologies-course 11.pptxWeb technologies-course 11.pptx
Web technologies-course 11.pptx
Stefan Oprea
 
Web technologies-course 09.pptx
Web technologies-course 09.pptxWeb technologies-course 09.pptx
Web technologies-course 09.pptx
Stefan Oprea
 
Web technologies-course 08.pptx
Web technologies-course 08.pptxWeb technologies-course 08.pptx
Web technologies-course 08.pptx
Stefan Oprea
 
Web technologies-course 07.pptx
Web technologies-course 07.pptxWeb technologies-course 07.pptx
Web technologies-course 07.pptx
Stefan Oprea
 
Web technologies-course 06.pptx
Web technologies-course 06.pptxWeb technologies-course 06.pptx
Web technologies-course 06.pptx
Stefan Oprea
 
Web technologies-course 05.pptx
Web technologies-course 05.pptxWeb technologies-course 05.pptx
Web technologies-course 05.pptx
Stefan Oprea
 
Web technologies-course 04.pptx
Web technologies-course 04.pptxWeb technologies-course 04.pptx
Web technologies-course 04.pptx
Stefan Oprea
 
Web technologies-course 03.pptx
Web technologies-course 03.pptxWeb technologies-course 03.pptx
Web technologies-course 03.pptx
Stefan Oprea
 
Web technologies-course 02.pptx
Web technologies-course 02.pptxWeb technologies-course 02.pptx
Web technologies-course 02.pptx
Stefan Oprea
 
Web technologies-course 01.pptx
Web technologies-course 01.pptxWeb technologies-course 01.pptx
Web technologies-course 01.pptx
Stefan Oprea
 
Fundamentals of Digital Modulation.ppt
Fundamentals of Digital Modulation.pptFundamentals of Digital Modulation.ppt
Fundamentals of Digital Modulation.ppt
Stefan Oprea
 
Orthogonal Frequency Division Multiplexing.ppt
Orthogonal Frequency Division Multiplexing.pptOrthogonal Frequency Division Multiplexing.ppt
Orthogonal Frequency Division Multiplexing.ppt
Stefan Oprea
 
Modulation tutorial.ppt
Modulation tutorial.pptModulation tutorial.ppt
Modulation tutorial.ppt
Stefan Oprea
 
Comparison of Single Carrier and Multi-carrier.ppt
Comparison of Single Carrier and Multi-carrier.pptComparison of Single Carrier and Multi-carrier.ppt
Comparison of Single Carrier and Multi-carrier.ppt
Stefan Oprea
 
OFDM and MC-CDMA An Implementation using MATLAB.ppt
OFDM and MC-CDMA An Implementation using MATLAB.pptOFDM and MC-CDMA An Implementation using MATLAB.ppt
OFDM and MC-CDMA An Implementation using MATLAB.ppt
Stefan Oprea
 
Concepts of 3GPP LTE.ppt
Concepts of 3GPP LTE.pptConcepts of 3GPP LTE.ppt
Concepts of 3GPP LTE.ppt
Stefan Oprea
 
Multi-Carrier Transmission over Mobile Radio Channels.ppt
Multi-Carrier Transmission over Mobile Radio Channels.pptMulti-Carrier Transmission over Mobile Radio Channels.ppt
Multi-Carrier Transmission over Mobile Radio Channels.ppt
Stefan Oprea
 

More from Stefan Oprea (20)

Training-Book-Samsung.ppt
Training-Book-Samsung.pptTraining-Book-Samsung.ppt
Training-Book-Samsung.ppt
 
PRESENTATION ON TOUCH TECHNOLOGY.ppt
PRESENTATION ON TOUCH TECHNOLOGY.pptPRESENTATION ON TOUCH TECHNOLOGY.ppt
PRESENTATION ON TOUCH TECHNOLOGY.ppt
 
Web technologies-course 12.pptx
Web technologies-course 12.pptxWeb technologies-course 12.pptx
Web technologies-course 12.pptx
 
Web technologies-course 11.pptx
Web technologies-course 11.pptxWeb technologies-course 11.pptx
Web technologies-course 11.pptx
 
Web technologies-course 09.pptx
Web technologies-course 09.pptxWeb technologies-course 09.pptx
Web technologies-course 09.pptx
 
Web technologies-course 08.pptx
Web technologies-course 08.pptxWeb technologies-course 08.pptx
Web technologies-course 08.pptx
 
Web technologies-course 07.pptx
Web technologies-course 07.pptxWeb technologies-course 07.pptx
Web technologies-course 07.pptx
 
Web technologies-course 06.pptx
Web technologies-course 06.pptxWeb technologies-course 06.pptx
Web technologies-course 06.pptx
 
Web technologies-course 05.pptx
Web technologies-course 05.pptxWeb technologies-course 05.pptx
Web technologies-course 05.pptx
 
Web technologies-course 04.pptx
Web technologies-course 04.pptxWeb technologies-course 04.pptx
Web technologies-course 04.pptx
 
Web technologies-course 03.pptx
Web technologies-course 03.pptxWeb technologies-course 03.pptx
Web technologies-course 03.pptx
 
Web technologies-course 02.pptx
Web technologies-course 02.pptxWeb technologies-course 02.pptx
Web technologies-course 02.pptx
 
Web technologies-course 01.pptx
Web technologies-course 01.pptxWeb technologies-course 01.pptx
Web technologies-course 01.pptx
 
Fundamentals of Digital Modulation.ppt
Fundamentals of Digital Modulation.pptFundamentals of Digital Modulation.ppt
Fundamentals of Digital Modulation.ppt
 
Orthogonal Frequency Division Multiplexing.ppt
Orthogonal Frequency Division Multiplexing.pptOrthogonal Frequency Division Multiplexing.ppt
Orthogonal Frequency Division Multiplexing.ppt
 
Modulation tutorial.ppt
Modulation tutorial.pptModulation tutorial.ppt
Modulation tutorial.ppt
 
Comparison of Single Carrier and Multi-carrier.ppt
Comparison of Single Carrier and Multi-carrier.pptComparison of Single Carrier and Multi-carrier.ppt
Comparison of Single Carrier and Multi-carrier.ppt
 
OFDM and MC-CDMA An Implementation using MATLAB.ppt
OFDM and MC-CDMA An Implementation using MATLAB.pptOFDM and MC-CDMA An Implementation using MATLAB.ppt
OFDM and MC-CDMA An Implementation using MATLAB.ppt
 
Concepts of 3GPP LTE.ppt
Concepts of 3GPP LTE.pptConcepts of 3GPP LTE.ppt
Concepts of 3GPP LTE.ppt
 
Multi-Carrier Transmission over Mobile Radio Channels.ppt
Multi-Carrier Transmission over Mobile Radio Channels.pptMulti-Carrier Transmission over Mobile Radio Channels.ppt
Multi-Carrier Transmission over Mobile Radio Channels.ppt
 

Recently uploaded

PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Landownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptxLandownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptx
JezreelCabil2
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
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
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 

Recently uploaded (20)

PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Landownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptxLandownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
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)
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 

Web technologies-course 10.pptx

  • 1.
  • 2. COURSE 10 JAVASCRIPT lect. eng. Rajmond JÁNÓ, PhD rajmond.jano@ael.utcl uj.ro fb.com/janorajmond
  • 3. C10 – JAVASCRIPT • JSON • AJAX
  • 4. JSON • JSON stands for JavaScript Object Notation • JSON is a syntax for storing and exchanging data • JSON is text, written with JavaScript object notation • JSON is "self-describing" and easy to understand • JSON is language independent • JSON uses JavaScript syntax, but the JSON format is text only • Text can be read and used as a data format by any programming language
  • 5. JSON SYNTAX JSON syntax is derived from JavaScript object notation syntax: • Data is in name/value pairs • Data is separated by commas • Curly braces hold objects • Square brackets hold arrays
  • 6. JSON DATA • JSON data is written as name/value pairs • A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value: JSON JavaScript JSON always uses double quotes
  • 7. JSON FILES • The file type for JSON files is ".json"
  • 8. JSON VS XML • Both JSON and XML can be used to receive data from a web server JSON XML
  • 9. JSON VS XML • XML is much more difficult to parse than JSON • JSON is parsed into a ready-to-use JavaScript object • For AJAX applications, JSON is faster and easier than XML • Using XML • Fetch an XML document • Use the XML DOM to loop through the document • Extract values and store in variables • Using JSON • Fetch a JSON string • JSON.Parse the JSON string
  • 10. JSON DATA TYPES In JSON, values must be one of the following data types: • a string • a number • an object (JSON object) • an array • a boolean • null JSON values cannot be one of the following data types: • a function • a date
  • 11. JSON OBJECTS • JSON objects are surrounded by curly braces {} • JSON objects are written in key/value pairs • Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null) • Keys and values are separated by a colon • Each key/value pair is separated by a comma
  • 12. NESTED JSON OBJECTS • Values in a JSON object can be another JSON object
  • 13. ACCESSING OBJECT VALUES • You can access the object values by using dot (.) notation
  • 14. LOOPING THROUGH AN OBJECT • You can loop through object properties by using the for-in loop
  • 15. LOOPING THROUGH AN OBJECT • In a for-in loop, use the bracket notation to access the property values
  • 16. UPDATING AND DELETING VALUES • You can use the dot notation to modify any value in a JSON object • You can also use the bracket notation to modify a value in a JSON object • Use the delete keyword to delete properties from a JSON object
  • 17. JSON PARSE • A common use of JSON is to exchange data to/from a web server • When receiving data from a web server, the data is always a string • Parse the data with JSON.parse(), and the data becomes a JavaScript object
  • 19. JSON STRINGIFY • When sending data to a web server, the data has to be a string • Convert a JavaScript object into a string with JSON.stringify()
  • 20. AJAX AJAX is used to • Read data from a web server - after the page has loaded • Update a web page without reloading the page • Send data to a web server - in the background
  • 21. AJAX • AJAX stands for Asynchronous JavaScript And XML • AJAX is not a programming language • AJAX just uses a combination of: • A browser built-in XMLHttpRequest object (to request data from a web server) • JavaScript and HTML DOM (to display or use the data) • AJAX is a misleading name • AJAX applications might use XML to transport data, but it is more common to transport data as plain text or JSON text • AJAX allows web pages to be updated asynchronously by exchanging data with a web server behind the scenes • This means that it is possible to update parts of a web page, without reloading the whole page
  • 22. AJAX Data request An event occurs: • Create an XMLHttpRequest object • Send HttpRequest Backend • Process HttpRequest • Create a response and send back to the browser Data processing • Process the returned data using JavaScript • Update page content Interne t Interne t Browser Server
  • 23. THE XMLHTTPREQUEST OBJECT • The keystone of AJAX is the XMLHttpRequest object • All modern browsers support the XMLHttpRequest object • The XMLHttpRequest object can be used to exchange data with a web server behind the scenes • This means that it is possible to update parts of a web page, without reloading the whole page
  • 24. THE XMLHTTPREQUEST OBJECT • Syntax for creating an XMLHttpRequest object • For security reasons, modern browsers do not allow access across domains • This means that both the web page and the XML file it tries to load, must be located on the same server
  • 25. XMLHTTPREQUEST OBJECT METHODS Method Description new XMLHttpRequest() Creates a new XMLHttpRequest object abort() Cancels the current request getAllResponseHeaders() Returns header information getResponseHeader() Returns specific header information open(method, url, async, user, psw) Specifies the request method: the request type GET or POST url: the file location async: true (asynchronous) or false (synchronous) user: optional username psw: optional password send() Sends the request to the server Used for GET requests send(string) Sends the request to the server. Used for POST requests setRequestHeader() Adds a label/value pair to the header to
  • 26. XMLHTTPREQUEST OBJECT PROPERTIES Property Description onreadystatechange Defines a function to be called when the readyState property changes readyState Holds the status of the XMLHttpRequest 0: request not initialized 1: server connection established 2: request received 3: processing request 4: request finished and response is ready responseText Returns the response data as a string responseXML Returns the response data as XML data status Returns the status-number of a request 200: "OK" 403: "Forbidden" 404: "Not Found“ … etc. statusText Returns the status-text (e.g. "OK" or "Not
  • 27. AJAX – GETTING DATA FROM A SERVER • To send a request to a server, we use the open() and send() methods of the XMLHttpRequest object • We then used the GET method, to download data from the server
  • 28. AJAX – SENDING DATA TO A SERVER • To send a request to a server, we use the open() and send() methods of the XMLHttpRequest object • We then used the POST method, to upload data to the server
  • 29. AJAX – SERVER RESPONSE • The onreadystatechange property • The readyState property holds the status of the XMLHttpRequest • The onreadystatechange property defines a function to be executed when the readyState changes • The status property and the statusText property holds the status of the XMLHttpRequest object • The onreadystatechange function is called every time the readyState changes
  • 30. AJAX – SERVER RESPONSE • Using a Callback Function • A callback function is a function passed as a parameter to another function • If you have more than one AJAX task in a website, you should create one function for executing the XMLHttpRequest object, and one callback function for each AJAX task • The function call should contain the URL and what function to call when the response is ready
  • 31. AJAX – SERVER RESPONSE
  • 32. AJAX – SERVER RESPONSE • Alternatively, the onload function can be used • The onload function is called every time data is available from the server
  • 33. JSON & AJAX – EXAMPLE 01 • Design a webpage that on every button press loads 3 pieces of data
  • 34. JSON & AJAX – EXAMPLE 01 • To get data, we are going to use the “Users” section of the https://jsonplaceholder.typicode.com page • To get all the comments we can access https://jsonplaceholder.typicode.com/users/
  • 35. JSON & AJAX – EXAMPLE 01
  • 36. JSON & AJAX – EXAMPLE 01
  • 37. JSON & AJAX – EXAMPLE 01
  • 38. JSON & AJAX – EXAMPLE 01
  • 39. JSON & AJAX – EXAMPLE 01
  • 40. JSON & AJAX – EXAMPLE 01 Why?
  • 41. JSON & AJAX – EXAMPLE 01
  • 42. JSON & AJAX – EXAMPLE 01
  • 43. JSON & AJAX – EXAMPLE 01 Why?
  • 44. JSON & AJAX – EXAMPLE 01
  • 45. JSON & AJAX – EXAMPLE 01
  • 46. JSON & AJAX – EXAMPLE 01
  • 47. JSON & AJAX – EXAMPLE 01
  • 48. JSON & AJAX – EXAMPLE 02 • Design a webpage with infinite scroll • Content is dynamically loaded whenever the user gets close to the bottom of the page (e.g.: Facebook, 9GAG, etc.)
  • 49. JSON & AJAX – EXAMPLE 02 • To get data, we are going to use the “Comments” section of the https://jsonplaceholder.typicode.com page • To get all the comments we can access https://jsonplaceholder.typicode.com/comme nts/
  • 50. JSON & AJAX – EXAMPLE 02
  • 51. JSON & AJAX – EXAMPLE 02 • To get a single comment, we can access https://jsonplaceholder.typicode.com/comme nts/id
  • 52. JSON & AJAX – EXAMPLE 02 • Most HTML will be dynamically generated, so our starting index.html file is very simple
  • 53. JSON & AJAX – EXAMPLE 02 • Now, we must design how we want a comment to look like on our webpage h1 with id of “name” h3 with id of “email” h1 with id of “label” p with id of “message” scroll position counters (for debugging only)
  • 54. JSON & AJAX – EXAMPLE 02
  • 55. JSON & AJAX – EXAMPLE 02 • First, we want to cache the DOM • Then we want to load at least some data onto our webpage: we’ll load initially 10 pieces of data
  • 56. JSON & AJAX – EXAMPLE 02 • The renderComment(cnt) function gets the data from the server and generates the HTML from it
  • 57. JSON & AJAX – EXAMPLE 02 • The renderComment(cnt) function gets the data from the server and generates the HTML from it
  • 58. JSON & AJAX – EXAMPLE 02 Why isn’t my data in the correct order?
  • 59. JSON & AJAX – EXAMPLE 02 • An AJAX request is asynchronous by default • It does not wait for the server response before executing the next line of code → data that arrives earlier will be the first to be displayed • Can this be fixed? • Yes, we can make the call synchronous
  • 60. JSON & AJAX – EXAMPLE 02
  • 61. JSON & AJAX – EXAMPLE 02 • In order to load new data when the user reaches the end of the page, we must determine when he reached the end of the page and take action (i.e.: load new data) • In order to achieve this, we “listen” every time the user scrolls: • Determine where the current scroll position is • Determine what is the maximum scrollable amount • If the current scroll positions is the maximum scrollable amount, then we know we must load new
  • 62. JSON & AJAX – EXAMPLE 02
  • 63. JSON & AJAX – EXAMPLE 02
  • 64. JSON & AJAX – EXAMPLE 02
  • 65. JSON & AJAX – EXAMPLE 02
  • 66. JSON & AJAX – EXAMPLE 02 • Identify possible code failure points
  • 67. BIBLIOGRAPHY • https://www.w3schools.com/js/js_json_intro.a sp • https://www.w3schools.com/js/js_ajax_intro.a sp • https://www.w3schools.com/js/js_window.asp • https://jsonplaceholder.typicode.com • http://www.filltext.com/
  • 68. COURSES Available online at: http://www.ael.utcluj.ro/ Information for Students -> Courses -> Web Technologies

Editor's Notes

  1. Failure points: No safeguard when no more data is available for loading (only 500 comments are available on JSONPlaceholder) Synchronous request makes code wait indefinitely if server is unreachable Not a failure, but rendering the HTML string is not the prettiest of things…