📂
SEI 1019
  • Introduction
  • About These Notes
  • Syllabus
  • Development Workflow
    • Command Line
      • The Terminal
      • Filesystem Navigation
      • File Manipulation
      • Additional Topics
    • Intro to Git
      • Version Control
      • Local Git
      • Remote Git
      • Git Recipes
    • Group Collaboration
      • Git Workflows
      • Project Roles and Tools
    • VS Code Tips & Tricks
  • HTML/CSS
    • HTML
    • CSS Selectors
    • CSS Box Model and Positioning
      • Box Model
      • Display and Positioning
      • Flexbox
      • Grid
      • Flexbox & Grid Games
      • Floats and Clears
      • Additional Topics
    • Advanced CSS
      • Responsive Design
      • Pseudo-Classes/Elements
      • Vendor Prefixes
      • Custom Properties
      • Additional Topics
    • Bootstrap
    • CSS Frameworks
    • Accessibility
  • JavaScript
    • Primitives
    • Arrays
    • Objects
    • Control Flow
      • Boolean Expressions
      • Conditionals
      • Loops
      • Promises
    • Functions
      • Callbacks
      • Timing Functions
      • Iterators
    • DOM and Events
    • DOM Manipulation
    • HTML5 Canvas
    • How To Reduce Redundancy
    • (2019) JavaScript OOP
    • (2016) OOP with Classes
    • (1995) OOP with Prototypes
      • Constructors
      • Prototypes
    • Intro to TDD
    • Scoping
    • Inheritance
      • Prototypal Inheritance
      • Call, Apply, and other Functions
      • ES6 Inheritance
      • Resources
    • Custom Node Modules
    • Additional Topics
      • AJAX, Fetch, and Async/Await
      • AJAX w/JSON and Localstorage
        • AJAX w/JSON
        • Local Storage
      • Async module
      • Data Scraping
  • jQuery
    • Intro
      • DOM Manipulation
      • Reddit Practice
      • Styling
      • Events
    • Plugins
    • AJAX
  • APIs
    • Fetch
    • AJAX w/jQuery
    • AJAX w/Fetch
  • Databases
    • Intro to SQL
    • Advanced SQL
    • MongoDB
      • Intro to NoSQL
      • CRUD in MongoDB
      • Data Modeling
      • Intermediate Mongo
  • Node/Express
    • Node
      • Intro to Node
      • Node Modules
      • Node Package Manager (NPM)
    • Express
      • Intro to Express
        • Routes
        • Views
        • Templates
        • Layouts and Controllers
        • CRUD & REST
          • Get and Post
          • Put and Delete
      • APIs with Express (request)
      • APIs with Express (axios)
    • Sequelize
      • Terminology
      • Setup
      • Using Models
      • Seeding Data
      • Validations and Migrations
      • Resources
      • 1:M Relationships
      • N:M Relationships
    • Express Authentication
      • Research Components
      • Code Components
      • Auth in Theory
        • Sessions
        • Passwords
        • Middleware
        • Hooks
      • Auth in Practice
        • Create the User
        • User Signup
        • Sessions
        • User Login
        • Authorization and Flash messages
    • Testing with Mocha and Chai
    • Mongoose
      • Mongoose Associations
    • JSON Web Tokens
      • Codealong
    • Additional Topics
      • oAuth
      • Geocoding with Mapbox
      • Geocoding and Google Maps
      • Cloudinary
      • Websockets with Socket.io
      • SASS
  • Ruby
    • Intro to Ruby
    • Ruby Exercises
    • Ruby Classes
    • Ruby Testing with Rspec
    • Ruby Inheritance
    • Ruby Data Scraping
  • Ruby on Rails
    • Intro to Rails
    • APIs with Rails
    • Asset Pipeline
    • Rails Auth and 1-M
      • Auth Components
    • Rails N:M
    • ActiveRecord Polymorphism
    • Additional Topics
      • oAuth
      • SASS
      • Rails Mailers
      • Cloudinary
      • Jekyll
  • React (Updated 2019)
    • ES6+/ESNext
      • Const and Let
      • Arrow Functions
      • Object Literals and String Interpolation
      • ES6 Recap
      • ES6 Activity
    • Intro to React
      • Create React App
      • Components and JSX
      • Virtual DOM
      • Props
      • Dino Blog Activity
      • Nested Components
      • Lab: LotR
    • React State
      • Code-Along: Mood Points
      • Code-Along: Edit Dino Blog
      • Lab: Simple Calc
      • Lifting State
    • React Router
      • Browser History/SPAs
      • React Router (lesson and full codealong)
      • Router Lab
    • Fetch and APIs
      • APIs with Fetch and Axios
      • Fetch the Weather
    • React Hooks
    • React LifeCycle
      • Lab: Component LifeCycle
    • React Deployment
    • Additional Topics
      • React Frameworks
        • Material UI Theming
      • Typescript
        • More Types and Syntax
        • Tsconfig and Declaration Files
        • Generics with Linked List
      • Redux
      • TypeScript
      • Context API
      • React Native
  • Meteor
  • Deployment and Config
    • Installfest
      • Mac OSX
      • Linux
      • Git Configuration
      • Sublime Packages
    • Deploy - Github Pages
    • Deploy - Node/Sequelize
    • Deploy - Node/MongoDB
    • Deploy React
    • Deploy - Rails
      • Foreman (Environment Variables)
    • Deploy - AWS Elastic Beanstalk
    • Deploy - S3 Static Sites
    • Deploy - Django
    • Deploy - Flask
  • Data Structures and Algorithms
    • Recursion
    • Problem Solving - Array Flatten
    • Binary Search
    • Algorithm Complexity
    • Stacks and Queues
    • Bracket Matching
    • Ruby Linked Lists
      • Sample Code
      • Beginner Exercises
      • Advanced Exercises
    • JS Linked Lists
      • Sample Code
      • Beginner Exercises
      • Beginner Solutions
    • Hash Tables
    • Intro to Sorting
    • Insertion Sort
    • Bucket Sort
    • Bubble Sort
    • Merge Sort
    • Quick Sort
    • Heap Sort
    • Sorting Wrapup
    • Hashmaps
    • Trees and Other Topics
  • Python
    • Python Installation
    • Intro to Python
    • Python Lists
    • Python Loops
    • Python Dictionaries
    • Python Sets and Tuples
    • Python Cheatsheet
    • Python Functions
    • Python Classes
    • Python Class Inheritance
    • Intro to Flask
    • Intro to SQLAlchemy
      • Flask and SQLAlchemy
    • Using PyMongo
    • Intro to Django
    • CatCollector CodeAlong
      • URLs, Views, Templates
      • Models, Migrations
      • Model Form CRUD
      • One-to-Many Relations
      • Many-to-Many Relations
      • Django Auth
    • Django Cheatsheet
    • Django Auth
    • Django Polls App Tutorial
    • Django School Tool Tutorial
    • Django 1:M Relationships
    • Custom Admin Views
    • Data Structures and Algorithms
      • Recursion
      • Binary Search
      • Stacks and Queues
      • Linked Lists
      • Binary Trees
      • Bubble Sort
      • TensorFlow & Neural Networks
    • Adjacent Topics
      • Raspberry Pi
      • Scripting
  • Assorted Topics
    • History of Computer Science
    • Regular Expressions
    • Intro to WDI (Course Info)
    • Being Successful in WDI
    • Internet Fundamentals
      • Internet Lab
    • User Stories and Wireframing
      • Wireframing Exercise: Build an Idea
    • Post WDI
      • Learning Resources
      • Deliverables -> Portfolio
      • FAQ
  • Projects
    • Project 1
    • Project 2
    • Project 3
      • Project 3 Pitch Guidelines
    • Project 4
    • Past Projects
      • Project 1
      • Project 2
      • Project 3
      • Project 4
      • Portfolios
    • Post Project 2
    • MEAN Hackathon
      • Part 1: APIs
      • Part 2: Angular
    • Portfolio
  • Web Development Trends
  • Resources
    • APIs and Data
    • Tech Websites
    • PostgreSQL Cheat Sheet
    • Sequelize Cheat Sheet
    • Database Administration
  • Archived Section
    • (Archived) ReactJS
      • Intro to React
        • Todo List Codealong
        • Additional Topics
      • Deploy React
      • React with Gulp and Browserify
        • Setting up Gulp
        • Additional Gulp Tasks
      • React Router
        • OMDB Router
        • OMDB Search
        • Additional Resources
      • React Animations
        • CSS Animations
    • AngularJS
      • Intro to AngularJS
        • Components and SPA
        • Create an Angular App
      • Angular Directives and Filters
      • Angular Animation
      • Angular Bootstrap Directives
        • Bootstrap Modals
      • Angular $http
      • Angular Services
        • Service Recipes
        • ngResource
        • Star Wars Codealong
      • Angular Routing
      • Angular + Express
      • Angular Authentication
        • Additional Topics
      • Angular Components
      • Angular Custom Filters
      • Angular Custom Directives
Powered by GitBook
On this page
  • Objectives
  • Understanding HTTP
  • AJAX
  • AJAX in Fetch
  • Promises
  • AJAX is asynchronous
  • Using Data
  • Putting AJAX On the Page
  • APIs to hit
  • Cross-Origin Requests

Was this helpful?

  1. APIs

AJAX w/Fetch

PreviousAJAX w/jQueryNextDatabases

Last updated 4 years ago

Was this helpful?

Objectives

  • Define AJAX and what the abbreviation stands for

  • Describe the purpose of AJAX

  • Utilize AJAX to fetch data from third-party APIs

  • Talk about the place of AJAX in the request-response cycle

  • Describe how promises work in the context of AJAX requests

AJAX (Asynchronous JavaScript and XML) is used to create asynchronous web applications. This simply means a web page that can make calls back to the server in the background without reloading the page.

Understanding HTTP

Before we can understand AJAX we need to get a little background on HTTP. HTTP is one of many internet protocols and is the protocol used for web communcation. HTTP can only send TEXT and is a request-response protocol. This means that a server can only respond to a request from a client (usually a web browser). .

AJAX

Originally the only way an HTTP request could be initiated was if the user clicked a link on a webpage, or submitted a form. AJAX allows JavaScript to send requests to the server with or without user interaction. This enables page content to be updated dynamically without a full page refresh.

AJAX in Fetch

There are many libraries that allow us to perform these AJAX requests. jQuery and Axios are a couple of the most popular implementations. However, there is one called fetch that is "nearly a standard" which means that we don't need any additional libraries to use it.

The most common AJAX methods are GET and POST. In this unit we'll mostly be using only GET but here is the distinction:

  • GET is used to request information from a URL.

  • POST is used to send information to a URL.

Basic GET example

fetch('https://www.reddit.com/search.json?q=kittens') 
  .then(function(responseData) {
    return responseData.json();
  })
  .then(function(jsonData) {
    console.log(jsonData);
  });

Let's look at this syntax. It uses some Promise functionality that we haven't seen, which we will get to shortly. The first line calls the fetch function and passes in the URL that we want to request data from. Then we use the .then part of the promise. Think of this as a callback - the function we pass into the .then function will only be executed when the API call has finished retrieving the data. This is actually really great because we don't know how long the API data will take to return. So we set up the next function to happen only after the data comes back. You can think of this as the same thing as a callback but it uses newer Promise syntax (the main advantage of which is the simplified syntax).

So the first thing we do once the data is returned is call responseData.json() and return the result of that. This is a required step to convert the data from the stringified version that comes from the API into an actual JavaScript object that we can use in code.

Then we use .then again to do something with the data after it is converted. In the example above, we simply console.log it.

Promises

Note that at the end of the AJAX request, there is a function called .then() that is called once the response has been received. This is an example of a promise. Promises are common concepts in JavaScript, and you can think of promises as a "contract" between two functions. When the .then() promise is attached to the AJAX function, it "promises" to run once the response comes back successful. This is due to the request-response cycle taking time, and requiring asynchronous behavior.

We will dig into Promises more deeply at a later date.

AJAX is asynchronous

An AJAX request doesn't allow you to load things instantly. Requesting things over the network always takes time, and you need an event handler to deal with the results. Look at the following code:

console.log('Script is running');

fetch('https://www.reddit.com/search.json?q=kittens') 
  .then(function(responseData) {
    return responseData.json();
  })
  .then(function(jsonData) {
    console.log("Here is the data:", jsonData);
  })

console.log('Just fired AJAX request!');

What order will the console.log statements appear?

Using Data

What if we want to use data from an AJAX request? We'll need to keep scope in mind, which is the concept of where variables exist.

Global variables are variables that are visible throughout the program, and thus have global scope

Local variables are variables that are visible only inside an immediate code block, and thus have local scope. Therefore, any variables we declare inside a function stop existing once we leave the function.

Some examples:

This code causes an error because posts only exists inside the immediate function its declared in

fetch('https://www.reddit.com/search.json?q=kittens')
  .then(function(data) {
    return data.json();
  })
  .then(function(json) {
    var posts = json;
});

console.log(posts);

// Output
// > Uncaught ReferenceError: posts is not defined(…)

This code runs because posts is declared in the same scope as data.

fetch('https://www.reddit.com/search.json?q=kittens')
  .then(function(data) {
    return data.json();
  })
  .then(function(json) {
    var posts = json;
    console.log(posts);
});

// Output
// > Object {kind: "Listing", data: Object}

Putting AJAX On the Page

It's no fun simply logging search results to the page. Let's put content on the page!

Let's see who is omn the International Space Station right now. First, we should make a project folder and create the correct folder structure:

  1. In terminal, create a new project directory in your code/Unit1 directory named iss-api

  2. Go into that directory and create a js folder

  3. Create your files: touch index.html and touch js/app.js

  4. Open the whole folder up in VS Code: code .

  5. Put in the HTML boilerplate and add your app.js script as the last element before the closing body tag.

  6. Add an AJAX call to the ISS API:

fetch('http://api.open-notify.org/astros.json')
  .then(function(data) {
    return data.json();
  })
  .then(function(json) {
    var posts = json;
    console.log(posts);
});

AJAX responses are usually deeply nested objects carry lots of information. We can look at an API's documentation to see what the response is supposed to look like. Orrr, we can set a breakpoint at the point when data arrives and use our debugger to play around with the object and see what data comes back.

The console.log() statement printing the response allows us to use our mouse to click on properties of the object and investigate them. Pausing the debugger at the point when the response arrives allows us to interact with the console when the response variable is in scope. We can type commands on the console to see if they work and then copy and paste valid code back into our program.

Remember, we have two ways to set breakpoints. We can write the debugger keyword in our program when we want the program to stop. Or, we can look at the source in Chrome's Developer Tools and click on a line to tell the browser to stop there.

Adding a console.log() will print the response every time, and gives us a line number to click on. We can follow the link to the line number and click on that line to set a temporary breakpoint manually.

Using the debugger keyword will stop our program every time. It won't output anything to the console so we won't be able to see data once we're past that point of execution.

Use your debugging tools to investigate the response object and find out how to access the title and the url of the first result. Copy and paste parts of this line of code into your program so you have easy access to the important parts of your search results.

Consider how the forEach and map functions may be useful tools to process these complicated objects.

Now you're cooking with gas.

APIs to hit

Here are a few APIs you can use to practice AJAX calls. They either won't save changes, or won't allow you to use POST, PUT or DELETE, so they're safe to play with.

Cross-Origin Requests

fetch('http://itunes.apple.com/search?term=arcade+fire')
  .then(function(data) {
    return data.json();
  })
  .then(function(json) {
    console.log(json);
  })

// output
// > No 'Access-Control-Allow-Origin' header is present on the requested resource.

For security reasons, browsers often restrict requests to other websites other than your own. However, some APIs like Reddit and the GA Doughnuts API can have their servers configured to allow these types of requests. The browser will know to not restrict requests because the server will send back additional information in a header. Here's an example you can try, if you'd like to see what's sent back.

In the terminal:

curl -I http://api.doughnuts.ga/doughnuts

HTTP/1.1 200 OK
Date: Wed, 27 Jan 2016 19:44:30 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 641
Connection: keep-alive
Set-Cookie: __cfduid=df8b7d51a2307f3b7bf782d1ce5d060011453923870; expires=Thu, 26-Jan-17 19:44:30 GMT; path=/; domain=.doughnuts.ga; HttpOnly
X-Powered-By: Express
Vary: Origin
Access-Control-Allow-Credentials: true
...

The header names sometimes vary, but because the Access-Control-Allow-Credentials was sent back by the server, the browser will let us receive data from this cross-origin request.

Note that not all websites/APIs play nice with AJAX. You may see an error in the console from APIs like the

Open Movie Database API
Acromine Acronym API
Pokemon API
Star Wars API
iTunes API
Read more on wiki
http diagram
debugging view