By the end of this, developers should be able to:
- Make HTTP requests to an API using
curl
and AJAX to:- (R)EAD resource collection.
- (R)EAD specific resource.
- (D)ELETE specific resource.
- (U)PDATE specific resource.
- (C)REATE new resource.
- Use response data in future requests.
- Fork and clone this repository. FAQ
- Create a new branch,
training
, for your work. - Checkout to the
training
branch. - Install dependencies with
npm install
. - Run your front-end server with
grunt serve
Verb | URI Pattern | Result |
---|---|---|
GET | /books |
read list of books |
GET | /books/:id |
read single book |
POST | /books |
create book |
PATCH | /books/:id |
update book |
DELETE | /books/:id |
destroy book |
We'll make requests to and receive responses from an HTTP server hosted at
https://wdi-library-api.herokuapp.com
.
If not already installed in chrome, let's add a JSON formatting utility.
Go to https://wdi-library-api.herokuapp.com/books
to read the list of books.
- Test API in browser (if possible)
- Test API with curl script
- Test API with AJAX
To implement AJAX, complete each step below, testing your web app along the way and using error-driven development to figure out where to go next:
- Adding the HTML:
- Add html form to
index.html
- Add input fields (if required) and a
submit
input to the form - Add appropriate name attribute to inputs
- Add input fields (if required) and a
- Creating the event listener:
- Attach an event listener that listens to the form submit event in
assets/scripts/app.js
.- Place within the document ready block.
- Creating the submit handler callback:
- Create a submit handler function in
assets/scripts/books/events.js
- Preventing default action?
- Getting form field values, if necessary?
-
Calling the API:
- Calling your api function:
- Make an API call using $.ajax in
assets/scripts/books/api.js
- Invoke this function within your submit handler callback.
- Make an API call using $.ajax in
- Handling response from calling the AJAX function:
- Are you using
.then()
and.catch()
and handling the successful and failure responses within each, respectively?- Easily check with just having
console.log
within each function's parenthesis. Then replace with your actual success and failure callbacks.
- Easily check with just having
- Are you using
- Calling your api function:
-
Handling API response:
- Create success and failure handler functions in
assets/scripts/ books/ui.js
- Pass submit handler as a callback to the event listener
- Create success and failure handler functions in
-
Test it out!
- Test the feature again. Getting any Errors?
- Double check each step. Use more console.logs. Notice and fix any errors in console. Not getting errors?
- You should see success or failure handler message on your page Don't see your messages on the page?
- Add a div to hold the message on the page
- Make sure to populate this div with content in your response handler
functions within
ui.js
Check out the get-form-fields markdown file for help
with using the getFormFields
function.
Wait, what is CRUD and why do you keep saying it?
CRUD is an acronym that stands for:
- Create
- Read
- Update
- Destroy
These four words describe the four basic functions of persistently storing data. They also closely relate to the HTTP verbs that we can use to interact with an API through requests. We'll discuss each of them as we code them out.
- browser
- curl
- AJAX
- browser
- curl
- AJAX
- browser(?)
- curl
- AJAX
- browser(?)
- curl
- AJAX
- browser(?)
- curl
- AJAX
- Input Validation: Javascript
- Input Validation: HTML5
- Clearing Forms
- All content is licensed under a CCBYNCSA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact legal@ga.co.