CSE 154 Lecture 22: AJAX.

Slides:



Advertisements
Similar presentations
9. AJAX & RIA. 2 Motto: O! call back yesterday, bid time return. — William Shakespeare.
Advertisements

6/3/2015eBiquity1 Tutorial on AJAX Anubhav Kale (akale1 AT cs DOT umbc DOT edu)
19-Jun-15 Ajax. The hype Ajax (sometimes capitalized as AJAX) stands for Asynchronous JavaScript And XML Ajax is a technique for creating “better, faster,
AJAX asynchronous server-client communication. Test.
Chapter 6 DOJO TOOLKITS. Objectives Discuss XML DOM Discuss JSON Discuss Ajax Response in XML, HTML, JSON, and Other Data Type.
Interactive Web Application with AJAX
Ajax (Asynchronous JavaScript and XML). AJAX  Enable asynchronous communication between a web client and a server.  A client is not blocked when an.
Ajax Basics The XMLHttpRequest Object. Ajax is…. Ajax is not…. Ajax is not a programming language. Ajax is not a programming language. Ajax is a methodology.
PHP and AJAX ISYS 475. AJAX Asynchronous JavaScript and XML: – JavaScript, Document Object Model, Cascade Style Sheet, XML, server-side script such as.Net,
JavaScript & jQuery the missing manual Chapter 11
CSCI 6962: Server-side Design and Programming Introduction to AJAX.
Lecture 12 – AJAX SFDV3011 – Advanced Web Development Reference: 1.
JavaScript, Fourth Edition Chapter 12 Updating Web Pages with AJAX.
AJAX and Java ISYS 350. AJAX Asynchronous JavaScript and XML: – Related technologies: JavaScript, Document Object Model, XML, server-side script such.
Intro to Ajax Fred Stluka Jan 25, /25/2006Intro to AjaxFred Stluka2 What is Ajax? "Asynchronous JavaScript and XML" New name for an old technique:
06/10/2015AJAX 1. 2 Introduction All material from AJAX – what is it? Traditional web pages and operation Examples of AJAX use Creating.
XMLHttpRequest Object When Microsoft Internet Explorer 5.0 introduced a rudimentary level of XML support, an ActiveX library called MSXML was also introduced,
Random Logic l Forum.NET l AJAX Behind the buzz word Forum.NET ● January 23, 2006.
Ajax In Action The Journey into Web2.0 Presented by Eric Pascarello.
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:
the acronym for Asynchronous JavaScript and XML.
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.
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.
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 AJAX Asynchronous JavaScript and XML --- MADHAVI
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 technology  Rich User Experience  Characteristics  Real live examples  JavaScript and AJAX  Web application workflow model – synchronous vs.
AJAX CS456 Fall Examples Where is AJAX used? Why do we care?
1 AJAX. AJAX – Whatzit? Asynchronous (content loading)‏ Javascript (logic & control)‏ And XML (request handling)‏
JQUERY AND AJAX
JavaScript, Sixth Edition Chapter 11 Updating Web Pages with Ajax.
JavaScript and Ajax Week 10 Web site:
CITA 330 Section 10 Web Remoting Techniques. Web Remoting Web Remoting is a term used to categorize the technique of using JavaScript to directly make.
NCCUCS 軟體工程概論 Lecture 5: Ajax, Mashups April 29, 2014.
AJAX. Objectives Understand and apply AJAX Using AJAX in DOJO library.
AJAX Rohan B Thimmappa. What Is AJAX? AJAX stands for Asynchronous JavaScript and XML. AJAX stands for Asynchronous JavaScript and XML. A remote scripting.
Introduction to AJAX Pat Morin COMP Outline What is AJAX? – History – Uses – Pros and Cons An XML HTTP Transaction – Creating an XMLHTTPRequest.
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.
CS 371 Web Application Programming
Data Virtualization Tutorial… CORS and CIS
AJAX and REST.
XMLHttp Object.
AJAX – Asynchronous JavaScript and XML
AJAX.
CSE 154 Lecture 11: AJAx.
AJAX Robin Burke ECT 360.
Tracking ClientSide Errors Techniques to Track “Blackbox” Errors
JavaScript & AJAX.
ISC440: Web Programming 2 AJAX
JavaScript & jQuery AJAX.
Chengyu Sun California State University, Los Angeles
Lecture 12: The Fetch Api and AJAx
AJAX CS-422 Dick Steflik.
Lecture 12: The Fetch Api and AJAx
Intro to Ajax Fred Stluka Jan 25, 2006.
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Carnegie Mellon Heinz College
Presentation transcript:

CSE 154 Lecture 22: AJAX

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

Web applications and Ajax web application: a dynamic website that mimics the feel of a desktop app presents a continuous user experience instead of disjoint pages examples: Gmail, Google Maps, Flickr Ajax: Asynchronous Javascript and XML not a programming language; a particular way of using Javascript downloads data from a server in the background allows dynamically updating a page without making the user wait avoids the “click, wait, refresh” pattern

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

The XMLHttpRequest object Javascript has an XMLHttpRequest object that can fetch files from a web server supported in IE7+, Safari, Firefox, Opera, Chrome, etc. IE5/6 don’t have it, but it’s 2015 Microsoft invented XMLHttpRequest and Ajax for use in an online version of Outlook in 1999 (credit where it’s due!) it can do this asynchronously (in the background, transparent to the user) the contents of the fetched file can be put into current web page using the DOM

A typical Ajax request user clicks, invoking an event handler handler’s code creates an XMLHttpRequest object XMLHttpRequest object requests page from server server retrieves appropriate data, sends it back XMLHttpRequest fires an event when data arrives this is often called a callback you can attach a handler function to this event your callback event handler processes the data and displays it

XMLHttpRequest methods the core Javascript object that makes Ajax possible Method Description open(method, url, async) specifies the URL and HTTP request method send() send(postData) sends the HTTP request to the server, with optional POST parameters abort() stops the request getAllResponseHeaders() getResponseHeader(name) setRequestHeader(name, value) for getting/setting raw HTTP headers

XMLHttpRequest properties Property Description responseText the entire text of the fetched page, as a string responseXML the entire contents of the fetched page, as an XML document tree (seen later) status the request’s HTTP status code (200 = OK, etc.) statusText HTTP status code text (e.g. “Bad Request” for 400) timeout how many MS to wait before giving up and aborting the request (default 0 = wait forever) readyState request’s current state (0 = uninitialized, 1 = set up, 2 = sent, 3 = in progress, 4 = complete)

Synchronized requests (bad) // this code is in some control's event handler var ajax = new XMLHttpRequest(); ajax.open("GET", url, false); ajax.send(); do something with ajax.responseText; JS create the request object, open a connection, send the request when send returns, the fetched text will be stored in request’s responseText property

Why synchronized requests suck your code waits for the request to completely finish before proceeding easier for you to program, but… the user’s entire browser LOCKS UP until the download is completed a terrible user experience (especially if the page is very large or slow to transfer) Deprecated in many major browsers: Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/. better solution: use an asynchronous request that notifies you when it is complete this is accomplished by learning about the event properties of XMLHttpRequest

XMLHttpRequest events Description load occurs when the request is completed error occurs when the request fails timeout occurs when the request times out abort occurs when the request is aborted by calling abort() loadstart, loadend, progress, readystatechange progress events to track a request in progress

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

Asynchronous requests, basic idea var ajax = new XMLHttpRequest(); ajax.onload = functionName; ajax.open("GET", url, true); ajax.send(); … function functionName() { if (this.status == 200) { do something with this.responseText; } else { code to handler 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

Handling the error event var ajax = new XMLHttpRequest(); ajax.onload = functionName; ajax.onerror = errorFunctionName; ajax.open("GET", url, true); ajax.send(); … function errorFunctionName(e) { do something with e, this.status, this.statusText, ... } JS the graceful way to handle errors is to listen for the error event the handler is passed the error/exception as a parameter you can examine the error, as well as the request status, to determine what went wrong

Example Ajax error handler var ajax = new XMLHttpRequest(); … ajax.onerror = ajaxFailure; function ajaxFailure(exception) { alert("Error making Ajax request:" + "\n\nServer status:\n" + this.status + " " + this.statusText + "\n\nServer response text:\n" + this.responseText); if (exception) { throw exception; } JS for user’s (and developer’s) benefit, show an error message if a request fails

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

Passing query parameters to a request var ajax = new XMLHttpRequest(); ajax.onload = functionName; ajax.open("GET", "url?name1=value1&name2=value2&...", true); ajax.send(); JS to pass parameters, concatenate them to the URL yourself you may need to URL-encode the parameters by calling the JS encodeURIComponent() function on them won’t work for POST requests (see next slide)

Creating a POST request var params = new FormData(); params.append("name", value); var ajax = new XMLHttpRequest(); ajax.onload = functionName; ajax.open("POST", "url", true); ajax.send(params); JS use a FormData object to gather your POST query parameters pass the FormData to the request’s send method method passed to open should be changed to “POST”

XMLHttpRequest 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