XMLHttpRequest. CS144: Web Applications

Similar documents
AJAX. Introduction. AJAX: Asynchronous JavaScript and XML

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


AJAX and PHP AJAX. Christian Wenz,

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


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

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

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

AJAX: Introduction CISC 282 November 27, 2018

Web Programming/Scripting: PHP and AJAX Refresher

Use of PHP for DB Connection. Middle and Information Tier

An Introduction to AJAX. By : I. Moamin Abughazaleh

AJAX: The Basics CISC 282 March 25, 2014

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

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

AJAX. Ajax: Asynchronous JavaScript and XML *

Session 11. Ajax. Reading & Reference

AJAX: The Basics CISC 282 November 22, 2017

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

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

Table of Contents. 1. A Quick Overview of Web Development...1 EVALUATION COPY

A synchronous J avascript A nd X ml

AJAX Programming Chris Seddon

AJAX ASYNCHRONOUS JAVASCRIPT AND XML. Laura Farinetti - DAUIN

Ajax. Ronald J. Glotzbach

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

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

Web Programming Step by Step

TIME SCHEDULE MODULE TOPICS PERIODS. HTML Document Object Model (DOM) and javascript Object Notation (JSON)

CSE 130 Programming Language Principles & Paradigms Lecture # 20. Chapter 13 Concurrency. + AJAX Discussion

Web Application Security

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

wemx WebService V1.0

Phase I. Initialization. Research. Code Review. Troubleshooting. Login.aspx. M3THOD, LLC Project Documentation

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

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

User Interaction: jquery

,

AJAX: Rich Internet Applications

Asynchronous JavaScript + XML (Ajax)

10.1 Overview of Ajax

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

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

Lesson 12: JavaScript and AJAX

WebApp development. Outline. Web app structure. HTML basics. 1. Fundamentals of a web app / website. Tiberiu Vilcu

AJAX เสถ ยร ห นตา ส าน กเทคโนโลย สารสนเทศและการส อสาร มหาว ทยาล ยนเรศวร พะเยา

JavaScript + PHP AJAX. Costantino Pistagna

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

Node.js. Node.js Overview. CS144: Web Applications

Chapter 3: Web Paradigms and Interactivity

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

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

JavaScript Programming

INDEX SYMBOLS See also

Front-end / Back-end. How does your application communicate with services?

Web 2.0 and Security

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

CSC Javascript

AJAX with APEX. For More Information:

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 :

/ Introduction to XML

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

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

function initcompleted() { settimeout('fbegin1()',300); } var allstudents = '';

Introduction to Ajax

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

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

Best Practices for Embedded User Assistance

AJAX: Asynchronous Event Handling Sunnie Chung

CS 5142 Scripting Languages

HTML 5 and CSS 3, Illustrated Complete. Unit L: Programming Web Pages with JavaScript

Ajax. David Matuszek's presentation,

1 Announcements (0:00 2:00) 2

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

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

Introduction to InfoSec SQLI & XSS (R10+11) Nir Krakowski (nirkrako at post.tau.ac.il) Itamar Gilad (itamargi at post.tau.ac.il)

EECS1012. Net-centric Introduction to Computing. Lecture "Putting It All Together" and a little bit of AJAX

Web Programming Paper Solution (Chapter wise)

ASP.NET AJAX adds Asynchronous JavaScript and XML. ASP.NET AJAX was up until the fall of 2006 was known by the code-known of Atlas.

JSON. Dr. Kanda Runapongsa Saikaew Computer Engineering Department

Working with Database. Client-server sides AJAX JSON Data formats Working with JSON data Request Response Bytes Database

Assignment: Seminole Movie Connection

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 Bringing Interactivity & Intuitiveness Into Web Applications. By : Bhanwar Gupta SD-Team-Member Jsoft Solutions

Using IBM Tivoli Monitoring V6.1 SOAP Services

So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object.

Controller/server communication

Controller/server communication

AJAX for Mobile Devices

Oracle WebLogic Portal

Ajax Application Design

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

ajax1.html 1/2 lectures/7/src/ ajax1.html 2/2 lectures/7/src/

Ajax in Oracle JDeveloper

The University of Bradford Institutional Repository

Building Dynamic Forms with XML, XSLT

IN Development in Platform Ecosystems Lecture 3: json, ajax, APIs

Ajax Hacks by Bruce Perry

Transcription:

XMLHttpRequest <show google suggest example interaction> http://oak.cs.ucla.edu/cs144/examples/google-suggest.html Q: What is going on behind the scene? What events does it monitor? What does it do when the event is detected? Q: When the "typing" event is detected, what does it have to do? How can it let users keep type while waiting for data from server? XMLHttpRequest: object for asynchronous communication with the server created differently depending on the browser IE 7+, non-ie: new XMLHttpRequest() IE 6+: new ActiveXObject("Msxml2.XMLHTTP") IE 5.5+: new ActiveXObject("Microsoft.XMLHTTP") e.g., xmlhttp = new XMLHttpRequest(); sending the request to the server: open() and send() methods xmlhttp.open("get", URL); // method, url xmlhttp.send(null); // optional body of the request Remark: same origin policy the request can be made only to the host of the web page cannot be used to get results from other web services handling server response Important properties of XMLHttpRequest elements: onreadystatechange: event handler function for the server response xmlhttp.onreadystatechange = handlerfunction; readystate: the status of the server s response 0: The request is not initialized 1: The request has been set up Junghoo "John" Cho (cho@cs.ucla.edu) 1

2: The request has been sent 3: The request is in process 4: The request is complete responsetext/responsexml: the data sent back from the server responsetext is text. responsexml is XML DOM <show Google suggest code. ask them read it> <html> <head> <script type="text/javascript"> var xmlhttp = new XMLHttpRequest(); // works only for Firefox, Safari,... // send Google suggest request based on the user input function sendajaxrequest(input) { var request = "google-suggest.php?q="+encodeuri(input); xmlhttp.open("get", request); xmlhttp.onreadystatechange = showsuggestion; xmlhttp.send(null); // update Web page with the response from Google suggest function showsuggestion() { if (xmlhttp.readystate == 4) { response = xmlhttp.responsetext; response = response.replace(/</g, "<"); response = response.replace(/>/g, ">"); document.getelementbyid("suggestion").innerhtml = response; Junghoo "John" Cho (cho@cs.ucla.edu) 2

</script> </head> <body> <b>your query:</b> <input type="text" onkeyup="sendajaxrequest(this.value);"><br/> <b>suggestion</b>: <pre id="suggestion"></pre> </body> </html> <let students to read the code. ask them questions on what it does and explain relevant parts of the code> Note this: the current element innerhtml: non-standard way of updating the text of an object Q: What events does it monitor? Q: What does it do when the event is detected? What URL does it use to send request? Q: When it receives response from the server, what does it do? Q: Could the XMLHttpRequest have been sent directly to Google? Note: same origin policy and the need for proxy XML and JSON in javascript <show google suggest v2 interaction> http://oak.cs.ucla.edu/cs144/examples/google-suggest2.html Remark: In most case, we have to process the response from the server and use part of it, instead of displaying it directly. How can we do it? Typeical server responses for AJAX applications: The server response is often in XML, but JSON is gaining popularity Junghoo "John" Cho (cho@cs.ucla.edu) 3

responsexml is the parsed XML DOM responsexml.documentelement: the root XML element JSON result should be processed from responsetext <show Google suggest v2 code> function showsuggestion() { if (xmlhttp.readystate == 4) { // get the CompleteSuggestion elements from the response var s = xmlhttp.responsexml.getelementsbytagname( CompleteSuggestion ); // construct a bullet list from the suggestions var htmlcode = "<ul>"; for (i = 0; i < s.length; i++) { var text = s[i].childnodes[0].getattribute("data"); htmlcode += "<li><b>" + text + "</b> (" + count + " queries)"; htmlcode += "</ul>"; // display the list on the page document.getelementbyid("suggestion").innerhtml = htmlcode; Q: How does it access the relevant part of response? JSON (Javascript object notation) The standard javascript syntax to represent "constant" e.g., [ { x: 3, y:"good", z:{ a:1, b:2, { x: 4, y:"bad", z:3 ] Q: What does the above notation mean in javascript? eval() function "evaluates" a text and return the results JSON text can be "parsed" into javascript objects through eval() Junghoo "John" Cho (cho@cs.ucla.edu) 4

var x = [ { x: 3, y:\"good\", z:{ a:1, b:2, \ { x: 4, y:\"bad\", z:3 ] ; var o = eval(x); once eval()ed, we can access its value as a standard javascript object var n = o[0].x + o[0].z.a + o[1].z; Q: What will be the value of n? Junghoo "John" Cho (cho@cs.ucla.edu) 5