AJAX + Web Services
CS 330: Intro to programming with JavaScript
1
Outline
2
1. Review: Template Literals
3
let name = 'Walter';
console.log( ` Some string.
Can be multiple lines.
Can have expressions ${2 + 2}� ${name} ` );
4
1. Review: Template Literals
5
2. Intro to AJAX
AJAX
Now Ajax (lowercase) as a generic term for any client-side process which fetches data from a server and updates the DOM dynamically without a full-page refresh.
6
2. Intro to Fetch
JavaScript’s Fetch API
7
Fetch Example
const url = 'https://www.apitutor.org/youtube/simple/?q=skateboarding+dog+&type=video';
fetch(url)� .then((response) => { � // takes a response stream and reads it to completion� return response.json(); � }) � .then((myJson) => { � // once entire data stream has been retrieved, you can execute� // code that makes use of the data� console.log(myJson); � });
8
Practice: Download Lecture Files
https://hci330.github.io/winter2020/course-files/lectures/lecture08.zip
9