ajax – a synchronous j avascript a nd x ml

21
1 AJAX – Asynchronous JavaScript and XML CS 236369, Spring 2010

Upload: amaya-nguyen

Post on 02-Jan-2016

31 views

Category:

Documents


1 download

DESCRIPTION

AJAX – A synchronous J avaScript a nd X ML. CS 236369, Spring 2010. Where Were We Before AJAX?. Static pages give the illusion of interactivity through standard form submissions. Form submissions result in full page loads. So, What’s The Problem?. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: AJAX –  A synchronous  J avaScript  a nd  X ML

1

AJAX – Asynchronous JavaScript and XML

CS 236369, Spring 2010

Page 2: AJAX –  A synchronous  J avaScript  a nd  X ML

2

Where Were We Before AJAX?

Static pages give the illusion of interactivity through standard form submissions.

Form submissions result in full page loads.

Page 3: AJAX –  A synchronous  J avaScript  a nd  X ML

3

So, What’s The Problem? Many actions only manipulate small portions of the

page but the entire page must be reloaded. Server responses contain the entire page content

rather than just the portion being updated. Loading entire pages typically results in several

additional HTTP requests for images, style sheets, scripts, and any other content that may be on the page.

Page 4: AJAX –  A synchronous  J avaScript  a nd  X ML

4

AJAX - Asynchronous JavaScript and XML A group of interrelated web development

techniques used on the client-side to create interactive web applications / rich Internet applications.

With Ajax, web applications can retrieve data from the server asynchronously in the background without interfering with the display and behavior of the existing page.

Thus, the Web page can communicate with the server without refreshing the whole page.

Page 5: AJAX –  A synchronous  J avaScript  a nd  X ML

5

Real-Life Examples of AJAX Apps Google maps

http://maps.google.com/ Goolgle Suggest (Now integrated in Google’s

homepage) http://www.google.com/webhp?complete=1&hl=en

Yahoo Maps http://maps.yahoo.com/

Many more…

Page 6: AJAX –  A synchronous  J avaScript  a nd  X ML

6

AJAX Components JavaScript

DOM XMLHttpRequest object (XHR) XML

Page 7: AJAX –  A synchronous  J avaScript  a nd  X ML

7

Ajax Fundamentals

Ajax uses a three-step process:1. Request a URL by the JavaScript code – client side.

2. Handle the URL on the server and write to the response – server side.

3. After the response is complete, integrate the response into the DOM (Document Object Model) – client side.

In an Ajax request we don't refresh the entire page; instead, we update only part of the page.

Page 8: AJAX –  A synchronous  J avaScript  a nd  X ML

8

The Server sideDid we reduce the load on the server? Ajax newcomers sometimes mistakenly believe

that Ajax, because it provides a more responsive user interface, reduces server-side traffic.

In fact, Ajax applications typically have more server-side traffic because each Ajax request involves a trip to the server. Because those requests are asynchronous, however,

Ajax creates the perception of a more responsive UI, though it typically does not reduce the load on the server.

Page 9: AJAX –  A synchronous  J avaScript  a nd  X ML

9

So, How Does It Work? JavaScript is used to:

Create and control instances of the XMLHttpRequest (XHR) object.

Provide handlers for responses. Manipulate the DOM.

The XMLHttpRequest object: Allows scripts to perform HTTP client functionality. Supports GET and POST operations.

Page 10: AJAX –  A synchronous  J avaScript  a nd  X ML

10

Launching HTTP Requests

Typically, 3 steps are required:

1.1. Construct and configure an XMLHttpRequest object

2.2. Launch the request

3.3. Process the response

Page 11: AJAX –  A synchronous  J avaScript  a nd  X ML

11

Constructing an XMLHttpRequest

In all modern browsers:

In older Microsoft browsers (IE 5 and 6):

(The XMLHttpRequest object is not specified in any W3C recommendation – it is not a W3C standard)

var request = new XMLHttpRequest();

var request = new ActiveXObject("Microsoft.XMLHTTP");

Page 12: AJAX –  A synchronous  J avaScript  a nd  X ML

12

Configuring an XMLHttpRequest

request.open("method","URL",isAsynchronous)

request.setRequestHeader("header","value")

• method is GET, POST, etc.

• URL must be in the domain of the current (or a relative URL), for security reasons

• The isAsynchronous boolean parameter will be discussed later

Page 13: AJAX –  A synchronous  J avaScript  a nd  X ML

13

Launching the Request

request.send(content )

• content is the posted in a POST request

• content can be null or empty

Page 14: AJAX –  A synchronous  J avaScript  a nd  X ML

Reading the Response

request.responseText

• The response as flat text

request.responseXML

• The response as a (DOM) Document object

• Available if response Content-Type is text/XML

request.status request.statusText

request.getAllResponseHeaders()

request.getResponseHeader("header")

The XHR Object

Page 15: AJAX –  A synchronous  J avaScript  a nd  X ML

15

<html>

< head>

< title<Jokes>/title>

< script type="text/javascript">

...2 slides ahead...

/< script>

/< head >

An Example

Page 16: AJAX –  A synchronous  J avaScript  a nd  X ML

>body onload="init(); setJoke()"<

>h2<Current date on server:>span id="serverTimeSpan“<? >/span<>h2<

>h1<Select a Joke:>/h1<

>p<>select onchange="presentServerTime();setJoke(value)"<

>option value="1" selected="selected"<Joke 1>/option<

>option value="2"<Joke 2>/option<

>option value="3"<Joke 3>/option<

>/select<>/p<

>div id="jokediv"<>/div<

>/body<

>/html<

An Example (Cont.)

Page 17: AJAX –  A synchronous  J avaScript  a nd  X ML

17

Page 18: AJAX –  A synchronous  J avaScript  a nd  X ML

18

Page 19: AJAX –  A synchronous  J avaScript  a nd  X ML

current-time.jsp

<%= new java.util.Date() %>

<%

long t0,t1;

t0=System.currentTimeMillis();

do{

t1=System.currentTimeMillis();

{

while (t1-t0<3000);//wait for 3 seconds

%>

19

Page 20: AJAX –  A synchronous  J avaScript  a nd  X ML

20

Example (Cont.)

Our examples use “false" in the third parameter of open(). This parameter specifies whether the request should be

handled asynchronously. As you can notice by running this example, the joke

only appear after the JSP response is received. True means that the script continues to run after

the send() method, without waiting for a response from the server.

Page 21: AJAX –  A synchronous  J avaScript  a nd  X ML

21

To be continued…