Lecture 12: The Fetch Api and AJAx

Slides:



Advertisements
Similar presentations
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Advertisements

© 2014 IBM Corporation Empowering the IBM ecosystem Introduction to Developing applications using node.js on Bluemix IBM Ecosystem Development Instructors.
JQUERY/AJAX ALIREZA KHATAMIAN DELARAM YAZDANSEPAS.
AJAX asynchronous server-client communication. Test.
Presented by…. Group 2 1. Programming language 2Introduction.
Lecture 3 – Data Storage with XML+AJAX and MySQL+socket.io
Ajax (Asynchronous JavaScript and XML). AJAX  Enable asynchronous communication between a web client and a server.  A client is not blocked when an.
SUNY Polytechnic Institute CS 490 – Web Design, AJAX, jQueryAngularJS AngularJS is a client-side JavaScript Framework for adding interactivity to HTML.
JavaScript & jQuery the missing manual Chapter 11
WaveMaker Visual AJAX Studio 4.0 Training Troubleshooting.
CSCI 6962: Server-side Design and Programming Introduction to AJAX.
IT 211 Project Integration and Deployment Lab #11.
06/10/2015AJAX 1. 2 Introduction All material from AJAX – what is it? Traditional web pages and operation Examples of AJAX use Creating.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Lecture 9: AJAX, Javascript review..  AJAX  Synchronous vs. asynchronous browsing.  Refreshing only “part of a page” from a URL.  Frameworks: Prototype,
AJAX Compiled from “AJAX Programming” [Sang Shin] (Asynchronous JavaScript and XML)
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (NON-Audio version) © Dr. David C. Gibbs WDMD.
AJAX Asynchronous JavaScript and XML 1. AJAX Outline What is AJAX? Benefits Real world examples How it works 2.
Event Handling & AJAX IT210 Web Systems. Question How do we enable users to dynamically interact with a website? Answer: Use mouse and keyboard to trigger.
CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…
Web Technology (NCS-504) Prepared By Mr. Abhishek Kesharwani Assistant Professor,UCER Naini,Allahabad.
JavaScript and Ajax Week 10 Web site:
NCCUCS 軟體工程概論 Lecture 5: Ajax, Mashups April 29, 2014.
AJAX. Objectives Understand and apply AJAX Using AJAX in DOJO library.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
REEM ALMOTIRI Information Technology Department Majmaah University.
Introduction to AJAX Pat Morin COMP Outline What is AJAX? – History – Uses – Pros and Cons An XML HTTP Transaction – Creating an XMLHTTPRequest.
NodeJS and MEAN cs6320.
Introduction to Java Import Scanner class to use in our program
Internet Technologies
JavaScript and Ajax (Ajax Tutorial)
CS 330 Class 7 Comments on Exam Programming plan for today:
CSE 154 Lecture 11: AJAx.
Not a Language but a series of techniques
Tutorial 10 Programming with JavaScript
Learning to Program D is for Digital.
Data Virtualization Tutorial… CORS and CIS
NodeJS and MEAN Prof. L. Grewe.
XMLHttp Object.
3 Things Everyone Knows About Node JS That You Don't
Introduction to Programming the WWW I
PHP / MySQL Introduction
2017, Fall Pusan National University Ki-Joune Li
Unit 27 - Web Server Scripting
CSE 154 Lecture 11: AJAx.
LGC Website, Software updates, Documentation, and Videos
CSE 154 Lecture 22: AJAX.
AJAX Robin Burke ECT 360.
Events Comp 205 Fall 2017.
ISC440: Web Programming 2 AJAX
JavaScript & jQuery AJAX.
Module P3 Practical: Building a webapp in nodejs and
Cordova & Cordova Plugin Installation and Management
Web Programming Language
Week 01 Node.js Week 01
Lecture 17: Web Service and post
Lecture 16: Writing Your Own Web Service
Lecture 12: The Fetch Api and AJAx
CSc 337 Lecture 1: post.
Lecture 14: JSON and Web SERVICES
Introduction to JavaScript
PHP and JSON Topics Review JSON.
CSc 337 Lecture 18: post.
Carnegie Mellon Heinz College
Lecture 15: Writing Your Own Web Service
How to install and manage exchange server 2010 OP Saklani.
Presentation transcript:

Lecture 12: The Fetch Api and AJAx CSc 337 Lecture 12: The Fetch Api and AJAx

Synchronous web communication synchronous: user must wait while new pages load the typical communication pattern used in web pages (click, wait, refresh)

Asynchronous web communication asynchronous: user can keep interacting with page while data loads

Asynchronous requests, basic idea var ajax = new XMLHttpRequest(); ajax.onload = functionName; ajax.open("GET", url, true); ajax.send(); ... function functionName() { do something with this.responseText; } JS attach an event handler to the load event handler will be called when request state changes, e.g. finishes function contains code to run when request is complete inside your handler function, this will refer to the ajax object you can access its responseText and other properties

What if the request fails? var ajax = new XMLHttpRequest(); ajax.onload = functionName; ajax.open("GET", "url", true); ajax.send(); ... function functionName() { if (this.status == 200) { // 200 means request succeeded do something with this.responseText; } else { code to handle the error; } } JS web servers return status codes for requests (200 means Success) you may wish to display a message or take action on a failed request

Promises Promise: A JS object that executes some code that has an uncertain outcome Promises have three states: - Pending - Fulfilled - Rejected Example: “I promise to post homework 4”  Pending: Not yet posted  Fulfilled: Homework 4 posted  Rejected: Wrong homework posted, or not posted in time 

Why are they better Help deal with uncertainty You determine whether it is fulfilled or rejected You define what happens when it fulfills or is rejected

Promise syntax function callAjax(){ var url = ..... // put url string here fetch(url) .then(checkStatus) .then(function(responseText) { //success: do something with the responseText }) .catch(function(error) { //error: do something with error }); }

Promise syntax function checkStatus(response) { if (response.status >= 200 && response.status < 300) { return response.text(); } else { return Promise.reject(new Error(response.status+": "+response.statusText)); }

Debugging Ajax code Firebug Net tab (or Chrome's Network tab) shows each request, parameters, response, errors expand a request with + and look at Response tab to see Ajax result check Console tab for any errors that are thrown by requests

Security restrictions Ajax must be run on a web page stored on a web server (cannot be run from a web page stored on your hard drive) Ajax can only fetch files from the same server that the page is on http://www.foo.com/a/b/c.html can only fetch from http://www.foo.com

Node.js We will be using node.js to run our server code (The code that generates what your fetch requests get) Download node.js: https://nodejs.org/en/ Double click on the downloaded file and follow the instructions to install it.

Running node.js You will need a command line to run node.js Window: you can use the built in command prompt, PowerShell (recommended), or download a command line like Cygwin Mac: there is a nice built in command line. Just search for "console" Linux: there is a nice built in command line

Testing that node is installed Open up your command line and type the following: node -v The version number of node on your machine should be output. If nothing is output or if there is an error node didn't install correctly.

Installing Express Run the following to install Express: npm install express

Node server Copy the following code into a file and name it server1.js // CSC 337 hello world server const express = require("express"); const app = express(); app.use(express.static('public')); app.get('/', function (req, res) { res.header("Access-Control-Allow-Origin", "*"); res.send('Hello World!'); }) app.listen(3000);

Running the node.js server You will need to navigate to the directory containing the file you created from the code on the last slide. To move between directories you can use: cd <directory> cd will move you into the directory specified. If you want to move into the parent directory, use .. as the directory name. If you want to see what directories exists in the directory you are in use ls to list them

Running the node.js server Once you have gotten to the directory that contains your server code, you can run it with the following command: node service1.js You will not see anything appear on the command line. To see it running open your browser and type the following in the address bar: http://localhost:3000/

Writing code to fetch from the server Write HTML and JavaScript to, when a button on the page is clicked, fetch data from the web server and inject that data into the page.