Understanding JSON, HTML Events and AJAX Requests
Understanding JSON, HTML Events and AJAX Requests
Requests
Understanding JSON
Objectives
Apply AJAX requests to load data between the client & the
02 AJAX
server
03 HTTP Request Apply Http request to exchange data with the server
HTML Events
Events (user or client side) are actions that browser reacts on
// HTML events
HTML events: JS can execute code on occurrence <button onclick="alert('Click event');">
of events Click
▪ Syntax: </button>
<element event="some JavaScript">
HTML elements have this 'event' handler attributes.
▪ Some common events: clicks
User
• onchange: an HTML element has been changed
• onclick: user clicks an HTML element Button
• onmouseover: user moves the mouse over an
HTML element Task onclick event
• onmouseout: user moves the mouse away from completed generated
an HTML element
• onkeydown: user pushes a keyboard key JavaScript
• onload: browser has finished loading the page click event
handling
onclick
event
listener
AJAX
AJAX: Asynchronous JavaScript And XML
01 AJAX GET & POST Use GET & POST in AJAX requests
▪ GET:
req.open("GET", URL?..., true/false);
sends data to server by adding data
to the server URL
URL?name1=val1&name2=val2&...
▪ POST:
req.open("POST", URL, true/false);
specification of data
req.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
the data to be sent
req.send("name1=val1&name2=val2&...");
Asynchronous
Let the JS process run while waiting for a response
false - synchronous
This will keep JS in wait mode till the
server responds
req.open("GET/POST", URL, false);
Recap
Understanding JSON
JSON Scope