An Introduction to AJAX. By : I. Moamin Abughazaleh

Similar documents
Session 18. jquery - Ajax. Reference. Tutorials. jquery Methods. Session 18 jquery and Ajax 10/31/ Robert Kelly,


A.A. 2008/09. What is Ajax?

Ajax. Ronald J. Glotzbach

Session 11. Ajax. Reading & Reference


AJAX: Introduction CISC 282 November 27, 2018

Ajax. David Matuszek's presentation,

AJAX: The Basics CISC 282 March 25, 2014

AJAX(Asynchronous Javascript + XML) Creating client-side dynamic Web pages

AJAX: The Basics CISC 282 November 22, 2017

Asynchronous JavaScript + XML (Ajax)

XMLHttpRequest. CS144: Web Applications

Module7: AJAX. Click, wait, and refresh user interaction. Synchronous request/response communication model. Page-driven: Workflow is based on pages

Fall Semester (081) Module7: AJAX

Introduction to AJAX Bringing Interactivity & Intuitiveness Into Web Applications. By : Bhanwar Gupta SD-Team-Member Jsoft Solutions

It is highly recommended that you are familiar with HTML and JavaScript before attempting this tutorial.

Contents. Demos folder: Demos\14-Ajax. 1. Overview of Ajax. 2. Using Ajax directly. 3. jquery and Ajax. 4. Consuming RESTful services

CS 5142 Scripting Languages

AJAX. Introduction. AJAX: Asynchronous JavaScript and XML

Term Paper. P r o f. D r. E d u a r d H e i n d l. H o c h s c h u l e F u r t w a n g e n U n i v e r s i t y. P r e s e n t e d T o :

Programming for Digital Media. Lecture 7 JavaScript By: A. Mousavi and P. Broomhead SERG, School of Engineering Design, Brunel University, UK

AJAX ASYNCHRONOUS JAVASCRIPT AND XML. Laura Farinetti - DAUIN

AJAX. Ajax: Asynchronous JavaScript and XML *

AJAX: Rich Internet Applications

Introduction to Ajax

CITS1231 Web Technologies. Ajax and Web 2.0 Turning clunky website into interactive mashups

Module 5 JavaScript, AJAX, and jquery. Module 5. Module 5 Contains 2 components

Developing Future Web Application with AJAX and ASP.NET Project "Atlas"

Introduction to Ajax. Sang Shin Java Technology Architect Sun Microsystems, Inc.

,

Web Programming Step by Step

Ajax or AJAX? The acronym AJAX has changed to the term Ajax, which does not represent specific technologies

Advanced Web Programming with JavaScript and Google Maps. Voronezh State University Voronezh (Russia) AJAX. Sergio Luján Mora

Ajax in Oracle JDeveloper

JavaScript CoAPRequest API

Ajax- XMLHttpResponse. Returns a value such as ArrayBuffer, Blob, Document, JavaScript object, or a DOMString, based on the value of

AJAX Programming Chris Seddon

JavaScript + PHP AJAX. Costantino Pistagna

Portlets and Ajax: Building More Dynamic Web Apps

10.1 Overview of Ajax

CSC Javascript

2/6/2012. Rich Internet Applications. What is Ajax? Defining AJAX. Asynchronous JavaScript and XML Term coined in 2005 by Jesse James Garrett

AJAX. Lecture 26. Robb T. Koether. Fri, Mar 21, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) AJAX Fri, Mar 21, / 16

Module 5 JavaScript, AJAX, and jquery. Module 5. Module 5 Contains an Individual and Group component

AJAX and PHP AJAX. Christian Wenz,

A synchronous J avascript A nd X ml

INDEX SYMBOLS See also

Getting Started. In this chapter

Abstract. 1. Introduction. 2. AJAX overview

COPYRIGHTED MATERIAL. AJAX Technologies. Google Suggest

Part of this connection identifies how the response can / should be provided to the client code via the use of a callback routine.

Credits: Some of the slides are based on material adapted from

Ajax Hacks by Bruce Perry

Pure JavaScript Client

AJAX Basics. Welcome to AJAX Basics presentation. My name is Sang Shin. I am Java technology architect and evangelist from Sun Microsystems.

What is Ajax? History of web interaction (2/2) Google Maps (1/2) History of Ajax (I) A cleaning powder. A Dutch football team.

CSC 443: Web Programming

Ajax Application Design

Use of PHP for DB Connection. Middle and Information Tier

Lesson 12: JavaScript and AJAX

Session 11. Calling Servlets from Ajax. Lecture Objectives. Understand servlet response formats

Controller/server communication

Chapter 1 Introduction to Computers and the Internet

Cross Site Request Forgery

Copyright Descriptor Systems, Course materials may not be reproduced in whole or in part without prior written consent of Joel Barnum

Controller/server communication

Chapter 3: Web Paradigms and Interactivity


CSC 498: Web Programming

Department of Computer Science Institute for System Architecture, Chair for Computer Networks. Interaction models in the World Wide Web


IT2353 WEB TECHNOLOGY Question Bank UNIT I 1. What is the difference between node and host? 2. What is the purpose of routers? 3. Define protocol. 4.

E ECMAScript, 21 elements collection, HTML, 30 31, 31. Index 161

Key features. Nothing to do with java It is the Client-side scripting language Designed to add interactivity to HTML pages

AJAX Programming Overview. Introduction. Overview

The course also includes an overview of some of the most popular frameworks that you will most likely encounter in your real work environments.

Introduction to Ajax

Developing Ajax Web Apps with GWT. Session I

Ajax UNIX MAGAZINE if0505.pdf. (86) Ajax. Ajax. Ajax (Asynchronous JavaScript + XML) Jesse James Garrett Web 1. Web.

1 Announcements (0:00 2:00) 2

GRITS AJAX & GWT. Trey Roby. GRITS 5/14/09 Roby - 1

Building Blocks of AJAX-Style Web Applications

THE NEW ERA OF WEB DEVELOPMENT. qooxdoo. Andreas Ecker, Derrell Lipman

AJAX: From the Client-side with JavaScript, Back to the Server

Comp 519: Web Programming Autumn 2014

Ajax HTML5 Cookies. Sessions 1A and 1B

AJAX IN THE CLASSROOM

UNIT 3 SECTION 1 Answer the following questions Q.1: What is an editor? editor editor Q.2: What do you understand by a web browser?

Use of PHP for DB Connection. Middle and Information Tier. Middle and Information Tier

FUSE Ajax Tutorial. 07/06 Version 1.2

Index. Ray Nicholus 2016 R. Nicholus, Beyond jquery, DOI /

CENG 490 STARSOFT. Requirement Analysis Report

AJAX. Lab. de Bases de Dados e Aplicações Web MIEIC, FEUP 2010/11. Sérgio Nunes

Web Application with AJAX. Kateb, Faris; Ahmed, Mohammed; Alzahrani, Omar. University of Colorado, Colorado Springs

Web Application Security

Networking & The Web. HCID 520 User Interface Software & Technology

REST AND AJAX. Introduction. Module 13

AJAX in Apache MyFaces A New Approach To Web Applications

Tutorial: Web Application Security

Transcription:

An Introduction to AJAX By : I. Moamin Abughazaleh

How HTTP works? Page 2 / 25

Classical HTTP Process Page 3 / 25 1. The visitor requests a page 2. The server send the entire HTML, CSS and Javascript code at once to the client 3. So, the communication is synchronious

Page 4 / 25 What is Javascript programming actually?

What is Javascript programming? Page 5 / 25 It is programming the browsers. So, we are limited to the objects that the browser presents us

Page 6 / 25 An Alternative for Managing requests - AJAX AJAX stands for Asynchronous JavaScript And XML. AJAX is based on XMLHttpRequest object of Javascript - so the browser and XMLHttpRequest is a standard http://www.w3.org/tr/xmlhttprequest/ It was introduced with IE-5.0 as an ActiveX object (1999) Later all the major browsers added XMLHttpRequest into their object bases.

Page 7 / 25 AJAX = Asynchronous JavaScript and XML It is a technique for creating better, faster, and more interactive web applications With XMLHttpRequest object JavaScript can trade data with a web server, without reloading the page AJAX uses asynchronous data transfer => allowing web pages to request small bits of information from the server instead of whole pages We can create desktop application like web applications using AJAX, this paradigm is also called WEB 2.0 programming

Page 8 / 25 AJAX - Based on Web Standards AJAX is based on the following web standards: XHTML and CSS Presentation DOM Dynamic display of and interaction with data XML and XSLT Tranfering data back and forth XMLHttpRequest Asynchronous transfer of data Javascript Bring these technologies together AJAX applications are browser and platform independent The XMLHttpRequest object is supported in Internet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Opera 8+, and Netscape 7.

How does it work? Page 9 / 25 Copyright 2009 Altuğ Tanaltay

How does it work? (cont d) <script type= text/javascript> Page 10 / 25 XMLHttpRequest function processresult() { get response from the server get object to be effected write the response function showcities() { new request object(url, parms) point to callback function (processresult()) send the request to server A onkeypressed New York Boston Washington PHP file that lists cites: processrequest(request params){ -get the request parameter -get data from the database -send the data to the client WEB SERVER

Advantages Page 11 / 25 Rich user experience Flexibility and accessibility Low bandwidth usage & fast responses Different platforms Increasing popularity with Web 2.0

Disadvantages Page 12 / 25 No browser history or back button, bookmarking Still using JavaScript, is JS disabled on the client? Search engine optimization problem Works only in the current domain Hard to debug and write code

Who are using AJAX? Page 13 / 25 AJAX was made popular in 2005 by Google (with Google Suggest). Facebook Gmail Google Maps and so on

XMLHttpRequest Object Page 14 / 25 Different browsers use different methods to create the XMLHttpRequest object Internet Explorer uses an ActiveXObject, most other browsers use XMLHttpRequest. So, in our javascript code we have to deal with the type of the browser when creating the request object

XMLHttpRequest Object(cont d) Page 15 / 25 XMLHttpRequest object is created with Javascript There are 3 ways to create this object: For most of the browsers: var request = new XMLHttpRequest(); For some IE versions: var request = new ActiveXObject("Microsoft.XMLHTTP"); For other versions of IE: var request = new ActiveXObject("Msxml2.XMLHTTP"); For the best result we shoult create all three

XMLHttpRequest Object Methods Page 16 / 25 Method abort() getallresponseheaders() getresponseheader("headerl abel") open("method", "URL"[, asyncflag[, "username"[, "password"]]]) send(content) setrequestheader("label", "value") Description Stops the current request Returns complete set of headers (labels and values) as a string Returns the string value of a single header label Assigns destination URL, method, and other optional attributes of a pending request Transmits the request, optionally with postable string or DOM object data Assigns a label/value pair to the header to be sent with a request

XMLHttpRequest Object Properties Page 17 / 25 Property onreadystatechange readystate responsetext responsexml status statustext Description Event handler for an event that fires at every state change Object status integer: 0 = uninitialized 1 = loading 2 = loaded 3 = interactive 4 = complete String version of data returned from server process DOM-compatible document object of data returned from server process Numeric code returned by server, such as 404 for "Not Found" or 200 for "OK" String message accompanying the status code Copyright 2009 Altuğ Tanaltay

Page 18 / 25 The Code (cont d) <script type="text/javascript"> function getxmlhttprequest() { var request; try { // Firefox, Opera 8.0+, Safari request = new XMLHttpRequest(); catch (e1) { // Internet Explorer try { request=new ActiveXObject("Msxml2.XMLHTTP"); catch (e2) { try { request=new ActiveXObject("Microsoft.XMLHTTP"); catch (e3) { alert("your browser does not support AJAX!"); return false; return request; </script> Copyright 2009 Altuğ Tanaltay

The Code (cont d) Page 19 / 25 After the creation of XMLHttpRequest object with the open method server request should be prepared: request.open(method, URL, async) method can be GET or POST URL determines where the request will be made If GET => parameters will be added to the end of the URL IF POST => parameters will be added in the package async (true/false), specifies whether the request should be handled asynchronously or not true is default

The Code (cont d) Page 20 / 25 After the creation of XMLHttpRequest it should be sent to the server with send() method request.send(null); Used if the method is GET request.send([data]); Used if the method is POST Example: request.send('var1=' +value1+'&var2='+value2);

The Code (cont d) Page 21 / 25 To get the response message from the server we should determine which method will work when the request object s onreadystatechange event is fired. request.onreadystatechange = somefunction; function somefunction() { if(request.readystate == 4){ var response = request.responsetext; //here do whatever with the response

Page 22 / 25 The Code (cont d) onreadystatechange Specifies a reference to an event handler for an event that fires at every state change readystate Returns the state of the object as follows: 0 = uninitialized open() has not yet been called. 1 = open send() has not yet been called. 2 = sent send() has been called, headers and status are available. 3 = receiving Downloading, responsetext holds partial data (although this functionality is not available in IE [3]) 4 = loaded Done. responsetext Returns the response as a string.

The Code - Using GET Page 23 / 25 var url = "get_data.php"; var params = "lorem=ipsum&name=binny"; http.open("get", url+"?"+params, true); http.onreadystatechange = function() { //Call a function when the state changes. if(http.readystate == 4 && http.status == 200) { alert(http.responsetext); http.send(null);

The Code - Using POST var url = "get_data.php"; var params = "lorem=ipsum&name=binny"; http.open("post", url, true); we choose post We have to set the content header Page 24 / 25 //Send the proper header information along with the request http.setrequestheader("content-type", "application/x-www-formurlencoded"); http.setrequestheader("content-length", params.length); http.setrequestheader("connection", "close"); http.onreadystatechange = function() { //Call a function when the state changes. if(http.readystate == 4 && http.status == 200) { alert(http.responsetext); http.send(params); Not NULL, and parameters aren t seperated with?

EXAMPLES Page 25 / 25