Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards:...

25
Ajax – hype oder hilfreich? orn Clausen [email protected]

Transcript of Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards:...

Page 1: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Ajax – hype oder hilfreich?

Jorn [email protected]

Page 2: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Ubersicht

• das World Wide Web im Wandel der Zeit

• XMLHttpRequest

• typische Probleme und Losungen

• Vor- und Nachteile

• Anwendung: Mashups mit Google Maps

Ajax – hype oder hilfreich? 2/24

Page 3: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

This is not the Web of your Youth

• Standards: XHTML + CSS ( + ECMAScript)

• Webservices, APIs

• Blogs, RSS, syndication

• Wikis, social networks, folksonomies

• RIA (Rich Internet Applications), Mashups

• Web 2.0

• Beispiele: Wikipedia, Google Mail/Maps, Flickr, del.icio.us, . . .

Ajax – hype oder hilfreich? 3/24

Page 4: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Web 1.0

Response

ServerBrowser

Request

Server

bloc

kier

tbl

ocki

ert

Browser

Ajax – hype oder hilfreich? 4/24

Page 5: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Interaktive Web-Anwendungen

• JavaScript + Frames (1995, Netscape 2.0)

• Dynamic HTML + DOM (1996, Internet Explorer 4.0)

• Iframes (1997, HTML 4.0)

• XMLHTTP (2001, Internet Explorer 5.0)

• XMLHttpRequest (2002, Mozilla 1.0)

• Ajax: A New Approach to Web Applications (2005, Jesse James Garrett)

Ajax – hype oder hilfreich? 5/24

Page 6: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Asynchronous JavaScript and XML

App.Server

Browser

EngineAjax-

Applikation Ajax-Engine Server

Ajax – hype oder hilfreich? 6/24

Page 7: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

XMLHttpRequest

• verschiedene Implementierungen von XHR, aber identisches API

• Unterschiede z.B. beim Caching von Antworten

• Standardisierung durch Web APIs WG innerhalb des W3C

• Frameworks und Toolkits

JavaScript Prototype, dojo, Sarissa, Adobe Spry, . . .

Java Google Web Toolkit (GWT), Apache Struts, . . .

PHP Sajax, Xajax

Python TurboGears

Ruby Ruby on Rails

.NET Microsoft Atlas

Perl CGI::Ajax, Nutzung von Prototype oder dojo

Ajax – hype oder hilfreich? 7/24

Page 8: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Beispiel, Server-Seite

• einfacher”case converter“

• Server-Seite per Shell-Skript

– lies Text von STDIN

– wandle Groß- in Kleinbuchstaben und umgekehrt

– schreibe nach STDOUT

• kann per CGI/POST benutzt werden

#!/bin/sh

echo "Content-Type: text/plain"

echo

tr a-zA-Z A-Za-z

Ajax – hype oder hilfreich? 8/24

Page 9: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Beispiel, HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Simple AJAX Test</title>

<script type="text/javascript" src=" chcase1.js "></script>

</head>

<body>

<form action="" method="post" onsubmit=" chcase() ;return false">

<div><input id=" input " size="50"/></div>

<div id=" output "></div>

</form>

</body>

</html>

Ajax – hype oder hilfreich? 9/24

Page 10: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Beispiel, Client-Seitevar http = null;

try { http = new ActiveXObject(’Msxml2.XMLHTTP’); }

catch (err) {

try { http = new ActiveXObject(’Microsoft.XMLHTTP’); }

catch (err) { http = null; }

}

if (!http && typeof XMLHttpRequest != ’undefined’) {

http = new XMLHttpRequest();

}

function chcase() {

var text = document.getElementById(’input’).value;

http.open(’POST’, ’/cgi-bin/chcase.sh’);

http.onreadystatechange = handleResponse;

http.send(text);

}

function handleResponse() {

if (http.readyState == 4 && http.status == 200) {

document.getElementById(’output’).innerHTML = http.responseText;

}

}

Ajax – hype oder hilfreich? 10/24

Page 11: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Wahrenddessen . . .

• Request durchlauft mehrere Phasen

4uninitialised sentopen receiving loaded

0 1 2 3

• handler wird bei jeder Zustandsanderung aufgerufenif ( http.readyState == 4 && http.status == 200) ...

• HTTP return codeif (http.readyState == 4 && http.status == 200 ) ...

var msg = http.statusText;

• HTTP header setzen und lesenhttp.setRequestHeader(’Pragma’, ’no-cache’);

var bestbefore = http.getResponseHeader(’Expires’);

Ajax – hype oder hilfreich? 11/24

Page 12: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Arbeiten mit XHR

• zunachst ungewohnt: Asynchronitat

• ahnlich zu Signal-Verarbeitung oder GUI-Programmierung

• Die Antwort ist 42! Was war die Frage?

<form action="" method="post" onsubmit="chcase( 1);return false">

<div><input id="input 1" size="50"/></div>

<div id="output 1"></div>

</form>

<hr/>

<form action="" method="post" onsubmit="chcase( 2);return false">

<div><input id="input 2" size="50"/></div>

<div id="output 2"></div>

</form>

Ajax – hype oder hilfreich? 12/24

Page 13: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Arbeiten mit XHR, cont.• funktioniert nicht:

function chcase( num) {

var text = document.getElementById(’input’ +num).value;

http.open(’POST’, ’/cgi-bin/chcase.sh’);

http.onreadystatechange = handleResponse (num) ;

http.send(text);

}

function handleResponse( num) {

if (http.readyState == 4 && http.status == 200) {

document.getElementById(’output’ +num).innerHTML = http.responseText;

}

}

• handler nur Funktionsname

• mogliche Losung: Identifikation in Request/Response

Ajax – hype oder hilfreich? 13/24

Page 14: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Arbeiten mit XHR, cont.• funktioniert nicht:

function chcase( num) {

var text = document.getElementById(’input’ +num).value;

http.open(’POST’, ’/cgi-bin/chcase.sh’);

http.onreadystatechange = handleResponse (num) ;

http.send(text);

}

function handleResponse( num) {

if (http.readyState == 4 && http.status == 200) {

document.getElementById(’output’ +num).innerHTML = http.responseText;

}

}

• handler nur Funktionsname

• mogliche Losung: Identifikation in Request/Response

• elegantere Losung: closurehttp.onreadystatechange = function() { handleResponse(num); } ;

Ajax – hype oder hilfreich? 13/24

Page 15: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Latenz

•”find as you type“: Google Suggest

• erster Versuch:<form action="" method="post" onkeyup ="chcase();return false">

<div><input id="input" size="50"/></div>

<div id="output"></div>

</form>

• neue Requests, bevor Responses eingetroffen sind

• XHR kann nur eine R/R-Transaktion gleichzeitig verarbeiten

• Losung: abort() und ein frisches XHR-Objekt

Ajax – hype oder hilfreich? 14/24

Page 16: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Latenz, cont.

var http=null;

function mkXHR () {

// browser sniffer

return XHR;

}

function chcase() {

if (http && http.readyState != 0) {

http.abort();

}

http = mkXHR();

if (http) {

http.open(’POST’, ’/cgi-bin/chcase.sh’);

http.onreadystatechange = handleResponse;

http.send(document.getElementById(’input’).value);

}

}

Ajax – hype oder hilfreich? 15/24

Page 17: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Response verarbeiten

• JSON

– JavaScript Object Notation

– mit eval() oder Parser in Datenstruktur umwandeln

• XML

– muss nicht, kann aber

– http.responseXML enthalt XML-Antwort als DOM-Objekt

– mit XSLT in (X)HTML umwandeln und darstellen

– Beispiel mit XSLT-Prozessor von Firefox/Mozilla/Sarissa

Ajax – hype oder hilfreich? 16/24

Page 18: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Film-Datenbank, HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Simple AJAX Film Database</title>

<script type="text/javascript" src="prototype.js"></script>

<script type="text/javascript" src="sarissa.js"></script>

<script type="text/javascript" src="films.js"></script>

<link href="films.css" type="text/css" rel="stylesheet"/>

</head>

<body onload="showFilms()">

<div id="films"></div>

<hr/>

<div id="roles"></div>

<hr/>

<div id="person"></div>

</body>

</html>

Ajax – hype oder hilfreich? 17/24

Page 19: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Film-Datenbank, Client-Seitevar xml = mkXHR();

var xslt = mkXHR();

function showFilms () {

xml.open(’GET’, ’/cgi-bin/filmdb.pl?q=films’);

xml.onreadystatechange = showFilms_H;

xml.send(null);

xslt.open(’GET’, ’films2html.xsl’);

xslt.onreadystatechange = showFilms_H;

xslt.send(null);

}

function showFilms_H {

if (xml.readyState == 4 && xml.status == 200 &&

xslt.readyState == 4 && xslt.status == 200) {

var transformer = new XSLTProcessor();

transformer.importStylesheet(xslt.responseXML);

html = transformer.transformToFragment(xml.responseXML, document);

$(’films’).innerHTML = ’’; $(’films’).appendChild(html);

} else {

$(’films’).innerHTML = ’please wait’;

}

}

Ajax – hype oder hilfreich? 18/24

Page 20: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Film-Datenbank, XML-Response

<?xml version=’1.0’ encoding=’ISO-8859-1’?>

<films>

<film>

<filmid>1</filmid>

<runtime>136</runtime>

<title>The Matrix</title>

<year>1999</year>

</film>

<film>

<filmid>2</filmid>

<runtime>251</runtime>

<title>The Lord of the Rings - The Return of the King</title>

<year>2003</year>

</film>

...

</films>

Ajax – hype oder hilfreich? 19/24

Page 21: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Film-Datenbank, XSLT<?xml version="1.0"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns="http://www.w3.org/1999/xhtml">

<xsl:template match="/films">

<table>

<xsl:apply-templates/>

</table>

</xsl:template>

<xsl:template match="film">

<tr>

<td><a href="javascript:showRoles({filmid})">

<xsl:value-of select="title"/>

</a></td>

<td><xsl:value-of select="year"/></td>

<td><xsl:value-of select="runtime"/> min.</td>

</tr>

</xsl:template>

</xsl:stylesheet>

Ajax – hype oder hilfreich? 20/24

Page 22: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Kritik• Benutzbarkeit

– Bruch mit klassischer Navigation (back button, bookmarks, . . . )

– Anwendung fast immer RESTless (Zustand nicht an URL gebunden)

– aber: gilt fur nicht-Web-Anwendungen genauso

– kurzere Reaktionszeiten, geringerer Datentransfer

• Barrierefreiheit

– Ziel ist oft Immitation klassischer GUIs

– inkrementeller Seitenaufbau problematisch fur Vorleser, Braille-Zeilen

– aber: vermutlich besser als Java-Applets oder Flash-Filme

• Sicherheit:

– relativ neue Technik

– Cross Site Scripting, JSON + eval() , . . .

Ajax – hype oder hilfreich? 21/24

Page 23: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Mashups

• Daten und Dienste konnen intern oder extern sein

Daten Dienst

intern intern Film-Quiz

intern extern GPS Tour Book

extern intern RSS Feed Reader

extern extern USGS Live Earthquake Mashup

• Problem bei externen Daten:”Same Origin Policy“ in JavaScript

• auf andere Sprachen/Methoden ausweichen (PHP, application proxy, . . . )

• mod proxy /mod rewrite auf dem lokalen Webserver

Ajax – hype oder hilfreich? 22/24

Page 24: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

USGS Live Earthquake Mashup

• RSS Feed vom USGS (United States Geological Survey)<item>

<pubDate>Sun, 22 Oct 2006 17:12:28 GMT</pubDate>

<title>M 2.9, Island of Hawaii, Hawaii</title>

<description>October 22, 2006 17:12:28 GMT</description>

<link>http://earthquake.usgs.gov/eqcenter/recenteqsww/Quakes/hv00021312.php</link>

<geo:lat>19.9292</geo:lat>

<geo:long>-155.9570</geo:long>

</item>

• lokalen URL fur Feed einrichtenRewriteRule ˆ/ajxp/usgseq-feed.xml

http://earthquake.usgs.gov/eqcenter/catalogs/feed.php?feed=eqs7day-M2.5.xml

[L,P]

• XHR in Google Maps APIGDownloadUrl(’/ajxp/usgseq-feed.xml’, showFeed);

Ajax – hype oder hilfreich? 23/24

Page 25: Ajax -- hype oder hilfreich? - oe-files.de fileThis is not the Web of your Youth • Standards: XHTML + CSS ( + ECMAScript) • Webservices, APIs • Blogs, RSS, syndication • Wikis,

Google Maps API

• gute Dokumentation

• einfach in eigene Web-Seiten einzubinden (nach vorheriger Registrierung)

• Overlays aus Punkten und Polygonzugen

• Popup-Fenster mit Text oder HTML

• XML-Parser, XSLT-Prozessor

• Geocoder

Ajax – hype oder hilfreich? 24/24