Introduction to AJAX Pat Morin COMP Outline What is AJAX? – History – Uses – Pros and Cons An XML HTTP Transaction – Creating an XMLHTTPRequest.

Slides:



Advertisements
Similar presentations
AJAX Presented by: Dickson Fu Dimas Ariawan Niels Andreassen Ryan Dial Jordan Nielson CMPUT 410 University of Alberta 2006.
Advertisements

1 JavaScript & AJAX CS , Spring JavaScript.
Introduction to AJAX AJAX Keywords: JavaScript and XML
Ajax (Asynchronous JavaScript and XML). AJAX  Enable asynchronous communication between a web client and a server.  A client is not blocked when an.
PHP and AJAX ISYS 475. AJAX Asynchronous JavaScript and XML: – JavaScript, Document Object Model, Cascade Style Sheet, XML, server-side script such as.Net,
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.
Ajax. –Asynchronous JavaScript and XML –Umbrella term for technologies that often: Use client-side scripting for layout and formatting Use less than full.
Ajax - 1h. AJAX IS: A browser technology A technology that uses only JavaScript in the browser page A very efficient way of updating information A way.
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.
Web Technology Introduction AJAXAJAX. AJAX Outline  What is AJAX?  Benefits  Real world examples  How it works  Code review  Samples.
School of Computing and Information Systems CS 371 Web Application Programming AJAX.
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
AJAX Asynchronous JavaScript & XML A short introduction.
SYST Web Technologies SYST Web Technologies AJAX.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
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.
CHAPTER 13 COMMUNICATING WITH AJAX. LEARNING OBJECTIVES AJAX, which stands for Asynchronous JavaScript and XMLprovides a way for a browser to send and.
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.
Dave Salinas. What is XML? XML stands for eXtensible Markup Language Markup language, like HTML HTML was designed to display data, whereas XML was designed.
AJAX AJAX Asynchronous JavaScript and XML --- MADHAVI
What is AJAX ? Asynchronous Javascript and XML. Not a stand-alone language or technology. It is a technique that combines a set of known technologies in.
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.
JavaScript, Sixth Edition Chapter 11 Updating Web Pages with Ajax.
JavaScript and Ajax Week 10 Web site:
NCCUCS 軟體工程概論 Lecture 5: Ajax, Mashups April 29, 2014.
Ajax SUBMITTED BY NITIN RAMANI C.S.E 3 rd Y 5 th S R.N CS SUBMITTED TO PRO. PUSHPARAJ PATEL SIR.
COMP2405 Review Pat Morin COMP Outline Web technologies and types of applications – Web sites (HTTP, HTML, CSS, CRAP) – Server-side scripted (CGI,
The Common Gateway Interface (CGI) Pat Morin COMP2405.
What is AJAX ? Asynchronous Javascript and XML.
JavaScript and Ajax (Ajax Tutorial)
CSE 154 Lecture 11: AJAx.
Not a Language but a series of techniques
AJAX AJAX = Asynchronous JavaScript and XML.
CS 371 Web Application Programming
Working with Client-Side Scripting
AJAX and REST.
XMLHttp Object.
AJAX – Asynchronous JavaScript and XML
AJAX.
AJAX.
Web System & Technology
CSE 154 Lecture 11: AJAx.
AJAX and JSP ISYS 350.
CSE 154 Lecture 22: AJAX.
AJAX Robin Burke ECT 360.
JavaScript & AJAX.
ISC440: Web Programming 2 AJAX
JavaScript & jQuery AJAX.
Chengyu Sun California State University, Los Angeles
AJAX CS-422 Dick Steflik.
Intro to Ajax Fred Stluka Jan 25, 2006.
DR. JOHN ABRAHAM PROFESSOR UTPA
Web Technology Even Sem 2015
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
AJAX Chapters 19, 20.
AJAX and JSP ISYS 350.
AJAX By Prof. B.A.Khivsara
Presentation transcript:

Introduction to AJAX Pat Morin COMP 2405

2 Outline What is AJAX? – History – Uses – Pros and Cons An XML HTTP Transaction – Creating an XMLHTTPRequest – Setting up an XMLHTTPRequest – Sending an XMLHTTPRequest Receiving an XML reply

3 What is AJAX? AJAX stands for Asynchronous Javascript And XML AJAX is not a programming language AJAX is a way of using existing standards (JavaScript and XML) to make more interactive web applications AJAX was popularized in 2005 by Google (with Google suggest)

4 An AJAX Application Recall the standard HTTP transaction – 1. Client opens connection to server – 2. Client sends request to server – 3. Server sends reply to client – 4. Client and server close connection After Step 4, the client renders the document and this may include running some JavaScript In an AJAX application, the JavaScript code then communicates with the server behind the scenes

5 An AJAX Application (Cont'd) Communication with the server takes place asynchronously, and transparently to the user Data is exchanged with the server without the need for a page reload This is accomplished through a special kind of HTTP request

6 Typical AJAX Event A typical AJAX transaction looks like this: 1. User triggers some event (presses a key, moves mouse,...) 2. Event handler code sends HTTP request to server 3. Server replies triggering code on client 4. Reply handler code updates web page using server's reply Between steps 2 and 3 the web page is still usable (event is asynchronous) At no point during the transaction does the browser open a new web page

7 Pros and Cons of AJAX Pros: – Allows web applications to interact with data on the server – Avoid clunky GET/POST send/receive interfaces – web apps look more and more like real applications – Some applications can only be realized this way Eg: Google Suggest offers interactive access to one of the largest data collections in the world – For office style applications, user's data is stored on a reliable server, accessable from any web browser Cons: – Tough to make compatible across all browsers – Should have a low-latency connection to the server – Can be server intensive Eg: Google Suggest generates a search for every keystroke entered

8 Setting up an AJAX Transaction Create an XMLHTTPRequest object Set up the request's onreadystatechange function Open the request Send the request

9 Creating an XMLHTTPRequest Object function sendRequest() var xmlHttp = GetXmlHttpObject(); if (!xmlHttp) { return false; } xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4) { alert("Request complete"); } var requestURI = " xmlHttp.open("GET", requestURI, true); xmlHttp.send(null); }

11 Setting onreadystatechange function sendRequest() var xmlHttp = GetXmlHttpObject(); if (!xmlHttp) { return false; } xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4) { alert("Request complete"); } var requestURI = " xmlHttp.open("GET", requestURI, true); xmlHttp.send(null); }

12 The open and send functions The open function of an XML HTTP request takes three arguments – xmlHttp.open(method, uri, async) – method is either "GET" or "POST" – uri is the (relative) URI to retrieve – async determines whether to send the request asynchronously (true) or synchronously (false) – The domain of the uri argument must be the same as the domain of the current page The send function takes one argument – xmlHttp.send(content); – content is the content to send (useful when method="POST")

13 Sending the Request function sendRequest() var xmlHttp = GetXmlHttpObject(); if (!xmlHttp) { return false; } xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4) { alert("Request complete"); } var requestURI = " xmlHttp.open("GET", requestURI, true); xmlHttp.send(null); }

14 The responseText Property When an XMLHTTPRequest is complete (readyState == 4) the responseText property contains the server's response, as a String

15 Example Code (Client Side) function sendRequest(textNode) var xmlHttp = GetXmlHttpObject(); if (!xmlHttp) { return false; } xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4) { textNode.nodeValue = xmlHttp.responseText; } var requestURI = " xmlHttp.open("GET", requestURI, true); xmlHttp.send(null); }

16 Example Code (Server Side) #!/usr/bin/perl print("Content-type: text/plain\n\n"); print("57 channels and nuthin' on"); And we might have the following request.cgi in the cgi-bin directory of greatbeyond.org

17 Some Notes An XMLHTTPRequest object can send the request to any URI as long as it has the same domain as the page that requests it This URI can refer to a CGI script or even just an HTML document Note the big security risk for the client – JavaScript can send anything to the server – Client needs to restrict what JavaScript has access to This is still not AJAX – Where's the XML?

18 Putting the X in AJAX The X in AJAX comes from XML In an XML HTTP request, we usually expect the server to respond with some XML What is XML? Short answer: like HTML but – You can make up your own tag names – All tags have to be closed (and there is a shorthand) Long answer: will have to wait

19 An Example XML File Tove Jani Reminder Don't forget me this weekend! Notice – the new tags (we just made them up) – An XML version number – One tag contains everything (and becomes the root of the document tree)

20 Why Respond with XML? We can look at the XML text within a response using the responseText property Even better, we can use the responseXML property to access the XML Best, responseXML.documentElement contains the document tree for the XML This is a document tree in the DOM model that we've seen before (just like document)

21 Example function sendRequest() { var xmlHttp = GetXmlHttpObject(); if (!xmlHttp) { return false; } xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4) { var xmlDoc = xmlHttp.responseXML.documentElement; } var requestURI = xmlURI; xmlHttp.open("GET", requestURI, true); xmlHttp.send(null); }

22 Summary An AJAX transaction involves the client sending an asynchronous HTTP request and the server responding with XML – The client processes the resulting XML document tree AJAX applications run entirely on the client except when they need to access data on the server – Can treat the server as a database/file system Well-written AJAX applications, running with a fast Internet connection, can be as nice to use as traditional applications (or nicer)