?print-pdf
' Created by
AJAX
Asynchronous
JavaScript
And
XML
In reality, these days, the use of JSON prevails over XML, but for backward compatibility we do not say AJAJ
With AJAX, Web applications can send and retrieve data from a Web server asynchronously (in the background) without refreshing the existent page
The traditional model of communication works synchronously.
The AJAX model of communication can work asynchronously.
With AJAX we can:
An empty DIV in which the data will be load, after the button click.
Note, that only the DIV content will be updated!
var nodes={
output: document.getElementsByClassName('output')[0],
getDataBtn: document.getElementsByClassName('getDataBtn')[0],
};
var dataURL = 'loremIpsum.txt';
var AJAXcall = function(){
// create a XMLHttpRequest object
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() :
new ActiveXObject("Microsoft.XMLHTTP");
// initializes the request:
xhr.open("GET", dataURL, true);
// EventHandler, that will be fired each time when the xhr state changes
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// finally do something with content:
nodes.output.innerHTML = this.responseText;
};
};
// sends the request:
xhr.send();
};
nodes.getDataBtn.addEventListener('click', function(){
AJAXcall();
});
then()
which accepts two callback functions and returns a promise object (which means that we can chain multiple then()
calls)
p.then(onFulfilled[, onRejected]);
p.then(value => {
// fulfillment
}, reason => {
// rejection
});
const response = fetch(url [, init])
const response = fetch('https://jsonplaceholder.typicode.com/todos/1')
console.dir(response);
// Promise
// [[Prototype]]: Promise
// catch: ƒ catch()
// constructor: ƒ Promise()
// finally: ƒ finally()
// then: ƒ then()
// Symbol(Symbol.toStringTag): "Promise"
// [[Prototype]]: Object
// [[PromiseState]]: "fulfilled"
// [[PromiseResult]]: Response
// with function declarations
fetch(url)
.then(function (response){
return response.text();
})
.then(function (data){
// do something with data
})
// with arrow function sntax
fetch(url)
.then( response=>response.text() )
.then( data => // do something with data )
const url = 'https://raw.githubusercontent.com/WWWCourses/ProgressBG-JS-Advanced-React-Slides/gh-pages/pages/themes/AJAX/data/charlotte_sometimes.txt';
fetch(url)
.then( response => response.text() )
.then( data => console.log(data) )
.catch( err=> console.log(err) )
fetch()
does not reject on HTTP errors — it only rejects on network errors.
// note that the path is wrong - there is no such file on github
const url = 'https://raw.githubusercontent.com/WWWCourses/ProgressBG-JS-Advanced-React-Slides/gh-pages/pages/themes/AJAX/data/charlotte_sometimes';
fetch(url)
.then( response => {
// console.dir(response);
if( response.ok ){
return response.text();
}else{
throw new Error(`Error: response status code is ${response.status}`)
}
} )
.then( data => console.log(data) )
.catch( err=> console.error(err.message))
https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js
axios.get(dataURL)
.then(function(response){
nodes.output.innerHTML = response.data;
}).catch(function(error){
console.log(error);
});
.txt
file for each song lyrics and make AJAX request to that file, on each image click.These slides are based on
customised version of
framework