Introduction To AJAX - Cglab.ca

1y ago
10 Views
3 Downloads
545.82 KB
22 Pages
Last View : 12d ago
Last Download : 3m ago
Upload by : Maxton Kershaw
Transcription

Introduction to AJAXPat MorinCOMP 2405

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 reply2

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

An AJAX Application Recall the standard HTTP transaction––––1. Client opens connection to server2. Client sends request to server3. Server sends reply to client4. Client and server close connection After Step 4, the client renders the document andthis may include running some JavaScript In an AJAX application, the JavaScript code thencommunicates with the server behind the scenes4

An AJAX Application (Cont'd) Communication with the server takes placeasynchronously, and transparently to the user Data is exchanged with the server without the needfor a page reload This is accomplished through a special kind ofHTTP request5

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 server3. Server replies triggering code on client4. 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 browseropen a new web page6

Pros and Cons of AJAX Pros:– Allows web applications to interact with data on the server– Avoid clunky GET/POST send/receive interfaces – web appslook more and more like real applications– Some applications can only be realized this way Eg: Google Suggest offers interactive access to one of thelargest data collections in the world– For office style applications, user's data is stored on a reliableserver, 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 every7

Setting up an AJAX Transaction Create an XMLHTTPRequest object Set up the request's onreadystatechangefunction Open the request Send the request8

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

The XMLHTTPRequest Object An XMLHTTPRequest object is in one of 5 states,as indicated by the readyState property0. The request is not initialized1. The request has been set up2. The request has been sent3. The request is in process4. The request is complete Every time the readyState property changes theonreadystatechange property (a function) iscalled10

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

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

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

The responseText Property When an XMLHTTPRequest is complete(readyState 4) the responseTextproperty contains the server's response, as aString14

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 tp.open("GET", requestURI, true);xmlHttp.send(null);}15

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

Some Notes An XMLHTTPRequest object can send the requestto any URI as long as it has the same domain asthe page that requests it This URI can refer to a CGI script or even just anHTML 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?17

Putting the X in AJAX The X in AJAX comes from XML In an XML HTTP request, we usually expect theserver 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 wait18

An Example XML File Notice– the new tags (we just made them up)– An XML version number– One tag contains everything (and becomes the root of thedocument tree) ?xml version "1.0" encoding "ISO-8859-1"? note to Tove /to from Jani /from heading Reminder /heading body Don't forget me this weekend! /body /note 19

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

Examplefunction 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);}21

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

Introduction to AJAX Pat Morin COMP 2405. 2 Outline What is AJAX? - History - Uses - Pros and Cons An XML HTTP Transaction - Creating an XMLHTTPRequest . In an AJAX application, the JavaScript code then communicates with the server behind the scenes. 5 An AJAX Application (Cont'd)

Related Documents:

Mastering Ajax, Part 6: Build DOM-based Web applications Mix the DOM and JavaScript -- those perfect Ajax companions-- to change a Web page's user interface without page reloads Skill Level: Intermediate Brett McLaughlin Author and Editor O'Reilly Media Inc. 12 Sep 2006 Combine the Document Object Model (DOM) with JavaScript code to create .

browsers. However, the jQuery team has taken care of this for us, so that we can write AJAX functionality with only one single line of code jQuery - AJAX load() Method jQuery load() Method The jQuery load() method is a simple, but powerful AJAX method. The load() method loads data from a server and puts the returned data into the selected element.

3.Web server process Ajax request and created http response 4.Ajax callback function renders data from web sever to div element on browser page Information retrieval using Ajax

A whole new generation WEB 2.0 is being written to take advantage of extreme dynamism as in AJAX. AJAX is used to build rich internet applications that are more interactive, responsive, and easy to use. With the advent of Ajax which involves extreme dynamism, novel pro

Ajax, Levy, Jayson, Seventh-Day Adventists Ontario Conference Ajax, Luscombe, Jason, The Pentecostal Assemblies Of Canada Eastern Ontario District Ajax,

Searching by category with two characters (ajax request is made when the search field count reaches 3 characters): (NOTE* I am using ajax request to prevent from page refresh, so when they user types in a search criteria parameter an ajax request is made to a php script that

This past year, Sophocles' Ajax was chosen as a prescribed text for the 2005 VCE Classical Societies and Cultures. It was an interesting choice, especially since the set text for the previ-ous few years was Sophocles' Oedipus Tyrannus (which, incidentally, still remains a pre-scribed text for VCE English).

sigurime, financë-kontabilitet, lidership dhe menaxhim burimesh njerëzore, administrim publik, lidership, e drejta publike, e drejta e biznesit, komunikim publik dhe gazetari ekonomike). Me VKM nr. 564 datë 28.05.2009 “Për hapjen e programeve të reja të studimit “Master i Nivelit të