Lecture 12: The Fetch Api and AJAx

Slides:



Advertisements
Similar presentations
Driving Test 1 Marking Scheme Focus on five areas to pass driving test 1.
Advertisements

JQUERY/AJAX ALIREZA KHATAMIAN DELARAM YAZDANSEPAS.
AJAX asynchronous server-client communication. Test.
User Interface Design using jQuery Mobile CIS 136 Building Mobile Apps 1.
Ajax (Asynchronous JavaScript and XML). AJAX  Enable asynchronous communication between a web client and a server.  A client is not blocked when an.
JavaScript & jQuery the missing manual Chapter 11
CSCI 6962: Server-side Design and Programming Introduction to AJAX.
AJAX and Java ISYS 350. AJAX Asynchronous JavaScript and XML: – Related technologies: JavaScript, Document Object Model, XML, server-side script such.
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.
Very Basic AJAX. Ajax the Great In Homer's Iliad he is described as of great stature, colossal frame, the tallest and strongest of all the Achaeans, second.
Lecture 9: AJAX, Javascript review..  AJAX  Synchronous vs. asynchronous browsing.  Refreshing only “part of a page” from a URL.  Frameworks: Prototype,
AJAX محمد احمدی نیا 2 Of 27 What is AJAX?  AJAX = Asynchronous JavaScript and XML.  AJAX is not a new programming language, but.
AJAX Compiled from “AJAX Programming” [Sang Shin] (Asynchronous JavaScript and XML)
Asynchronous Javascript And XML AJAX : an introduction UFCEUS-20-2 : Web Programming.
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Web Technologies Lecture 7 Synchronous vs. asynchronous.
AJAX Asynchronous JavaScript and XML 1. AJAX Outline What is AJAX? Benefits Real world examples How it works 2.
SE-2840 Dr. Mark L. Hornick 1 Introduction to Ajax Asynchronous Javascript And XML.
MIS 3200 – Unit 3.2 Page_Load method AutoPostBack property of controls IsPostBack method of the Page object Understanding web page life cycle.
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…
AJAX and REST. Slide 2 What is AJAX? It’s an acronym for Asynchronous JavaScript and XML Although requests need not be asynchronous It’s not really a.
AJAX – Asynchronous JavaScript And XML By Kranthi Kiran Nuthi CIS 764 Kansas State University.
Web Technology (NCS-504) Prepared By Mr. Abhishek Kesharwani Assistant Professor,UCER Naini,Allahabad.
AJAX CS456 Fall Examples Where is AJAX used? Why do we care?
JQUERY AND AJAX
JavaScript and Ajax Week 10 Web site:
NCCUCS 軟體工程概論 Lecture 5: Ajax, Mashups April 29, 2014.
JQuery, JSON, AJAX. AJAX: Async JavaScript & XML In traditional Web coding, to get information from a database or a file on the server –make an HTML form.
AJAX. Objectives Understand and apply AJAX Using AJAX in DOJO library.
Introduction to AJAX Pat Morin COMP Outline What is AJAX? – History – Uses – Pros and Cons An XML HTTP Transaction – Creating an XMLHTTPRequest.
Introduction to.
Internet Technologies
JavaScript and Ajax (Ajax Tutorial)
CSE 154 Lecture 11: AJAx.
Understanding XMLHttpRequest
Not a Language but a series of techniques
AJAX AJAX = Asynchronous JavaScript and XML.
AJAX and jQuery AJAX AJAX Concepts, XMLHttpRequest, jQuery AJAX: $.ajax(), $.get(), $.post() jQuery AJAX XMLHttpRequest SoftUni Team Technical Trainers.
Data Virtualization Tutorial… CORS and CIS
AJAX and REST.
Callback Function function passed as a parameter to another function
XMLHttp Object.
Unit 27 - Web Server Scripting
CSE 154 Lecture 11: AJAx.
CSE 154 Lecture 22: AJAX.
AJAX Robin Burke ECT 360.
Tracking ClientSide Errors Techniques to Track “Blackbox” Errors
jQuery form submission
ISC440: Web Programming 2 AJAX
JavaScript & jQuery AJAX.
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming More Node.js
AJAX CS-422 Dick Steflik.
Lecture 12: The Fetch Api and AJAx
CSc 337 Lecture 1: post.
CSC 143 Java Errors and Exceptions.
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
AJAX Chapters 19, 20.
Introduction to JavaScript
PHP and JSON Topics Review JSON.
CS5220 Advanced Topics in Web Programming More Node.js
CSc 337 Lecture 18: post.
Carnegie Mellon Heinz College
Promises.
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