Code Monkey home page Code Monkey logo

xhr-workshop's Introduction

xhr-workshop

Web APIs and xhr requests

An xhr request (XMLHttpRequest) is an API that lets you communicate data between a client and server.

We use the XMLHttpRequest object constructor to create requests via the browser (our client).

The XMLHttpRequest object gives you access to a number of methods that enable you to set up a request to another server.

Breakdown of an xhr request

There are four main parts to an xhr request:

1. Create an instance of the XMLHttpRequest object.

This will store your xhr request which you can access using the various properties and methods that come with it. see here var xhr = new XMLHttpRequest()

Properties we'll use: onreadystatechange, readyState, status, response (in the code along example)

Methods we'll use: open(), send()

2. Listen for a change to the status of your request

xhr.onreadystatechange = function() {
  if (xhr.readyState === 4 && xhr.status === 200) {
    // do something with the response
  }
}

The onreadystatechange property is an event handler that runs whenever the readyState of your request changes. You can assign it a function which gets executed upon change in readyState.

If the readyState property of your request is 4 that means the request is finished and the response is ready. more on onreadystate

However, you only want your function to run if the response contains the data you're after. You therefore need to check that the status property of the request is 200 (OK). Another common status is 404 (not found.) more on statuses

3.Initialise your request to the url you want to get data from

xhr.open('GET', url, true)

The url is usually that of another server's api. e.g. the starwars api http://swapi.co/api/

There are 3 parts to this method:

  • The Http request method. e.g. GET, POST, DELETE
  • The url you're making a request to
  • Whether or not your request is asynchronous (boolean)

4. Send the request

xhr.send()

Clone this repo for an example

git clone https://github.com/tas12/xhr-workshop.git && cd xhr-workshop

Resouces

Owen's xhr walkthrough - there are some good resources here too XMLHttpRequest docs

xhr-workshop's People

Contributors

tas12 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.