Asynchronous HTTP request generation in JavaScript (AJAX)

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 16 Introduction to Ajax.
Advertisements

9. AJAX & RIA. 2 Motto: O! call back yesterday, bid time return. — William Shakespeare.
19-Jun-15 Ajax. The hype Ajax (sometimes capitalized as AJAX) stands for Asynchronous JavaScript And XML Ajax is a technique for creating “better, faster,
AJAX asynchronous server-client communication. Test.
XMLHttpRequest Object and XML What we should learn in this lesson –What is the XHR object? –How to create the XHR objects? –XHR object properties –XHR.
CST JavaScript Validating Form Data with JavaScript.
The Document Object Model (DOM) & Asynchronous Javascript And XML (AJAX) : an introduction UFCEKG-20-2 : Data, Schemas and Applications.
INT222 – Internet Fundamentals Weekly Notes: AJAX and Canvas 1.
Interactive Web Application with AJAX
Ajax (Asynchronous JavaScript and XML). AJAX  Enable asynchronous communication between a web client and a server.  A client is not blocked when an.
CGI and AJAX CS-260 Dick Steflik.
Ajax Basics The XMLHttpRequest Object. Ajax is…. Ajax is not…. Ajax is not a programming language. Ajax is not a programming language. Ajax is a methodology.
PHP and AJAX ISYS 475. AJAX Asynchronous JavaScript and XML: – JavaScript, Document Object Model, Cascade Style Sheet, XML, server-side script such as.Net,
CSCI 6962: Server-side Design and Programming Introduction to AJAX.
Lecture 12 – AJAX SFDV3011 – Advanced Web Development Reference: 1.
Ajax - Part II Communicating with the Server. Learning Objectives By the end of this lecture, you should be able to: – Describe the overview of steps.
JavaScript, Fourth Edition Chapter 12 Updating Web Pages with AJAX.
AJAX and Java ISYS 350. AJAX Asynchronous JavaScript and XML: – Related technologies: JavaScript, Document Object Model, XML, server-side script such.
Intro to Ajax Fred Stluka Jan 25, /25/2006Intro to AjaxFred Stluka2 What is Ajax? "Asynchronous JavaScript and XML" New name for an old technique:
06/10/2015AJAX 1. 2 Introduction All material from AJAX – what is it? Traditional web pages and operation Examples of AJAX use Creating.
XMLHttpRequest Object When Microsoft Internet Explorer 5.0 introduced a rudimentary level of XML support, an ActiveX library called MSXML was also introduced,
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
Ajax In Action The Journey into Web2.0 Presented by Eric Pascarello.
Lecture 9: AJAX, Javascript review..  AJAX  Synchronous vs. asynchronous browsing.  Refreshing only “part of a page” from a URL.  Frameworks: Prototype,
AJAX محمد احمدی نیا 2 Of 27 What is AJAX?  AJAX = Asynchronous JavaScript and XML.  AJAX is not a new programming language, but.
AJAX Compiled from “AJAX Programming” [Sang Shin] (Asynchronous JavaScript and XML)
Fall 2006 Florida Atlantic University Department of Computer Science & Engineering COP 4814 – Web Services Dr. Roy Levow Part 2 – Ajax Fundamentals.
Web Database Programming Week 7 Session Management & Authentication.
Rails & Ajax Module 5. Introduction to Rails Overview of Rails Rails is Ruby based “A development framework for Web-based applications” Rails uses the.
Asynchronous Javascript And XML AJAX : an introduction UFCEUS-20-2 : Web Programming.
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
SYST Web Technologies SYST Web Technologies AJAX.
Web Security Lesson Summary ●Overview of Web and security vulnerabilities ●Cross Site Scripting ●Cross Site Request Forgery ●SQL Injection.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
AJAX Asynchronous JavaScript and XML 1. AJAX Outline What is AJAX? Benefits Real world examples How it works 2.
SE-2840 Dr. Mark L. Hornick 1 Introduction to Ajax Asynchronous Javascript And XML.
AJAX. Overview of Ajax Ajax is not an API or a programming language Ajax aims to provide more responsive web applications In normal request/response HTTP.
AJAX and REST. Slide 2 What is AJAX? It’s an acronym for Asynchronous JavaScript and XML Although requests need not be asynchronous It’s not really a.
AJAX – Asynchronous JavaScript And XML By Kranthi Kiran Nuthi CIS 764 Kansas State University.
Internet Technologies #6 REST SOAP AJAX. Agenda  REST  SOAP  AJAX.
INNOV-2: Build a Better Web Interface Using AJAX Chris Morgan Pandora Software Systems
Web Technology (NCS-504) Prepared By Mr. Abhishek Kesharwani Assistant Professor,UCER Naini,Allahabad.
 AJAX technology  Rich User Experience  Characteristics  Real live examples  JavaScript and AJAX  Web application workflow model – synchronous vs.
Unit-6 Handling Sessions and Cookies. Concept of Session Session values are store in server side not in user’s machine. A session is available as long.
1 AJAX. AJAX – Whatzit? Asynchronous (content loading)‏ Javascript (logic & control)‏ And XML (request handling)‏
Javascript AJAX HTML WEB SERVER Asynchronous. Javascript HTML events DOM – document object model browser internal view of html page compute.
JavaScript, Sixth Edition Chapter 11 Updating Web Pages with Ajax.
A New Way To Web Applications Development Tin Htut Htut Naing Oo Myanmar Information Technology.
JavaScript and Ajax Week 10 Web site:
CITA 330 Section 10 Web Remoting Techniques. Web Remoting Web Remoting is a term used to categorize the technique of using JavaScript to directly make.
NCCUCS 軟體工程概論 Lecture 5: Ajax, Mashups April 29, 2014.
JavaScript and Ajax (Ajax Tutorial)
CSE 154 Lecture 11: AJAx.
Not a Language but a series of techniques
AJAX and REST.
XMLHttp Object.
CSE 154 Lecture 11: AJAx.
AJAX and JSP ISYS 350.
CSE 154 Lecture 22: AJAX.
AJAX Robin Burke ECT 360.
JavaScript & AJAX.
ISC440: Web Programming 2 AJAX
JavaScript & jQuery AJAX.
Chengyu Sun California State University, Los Angeles
AJAX CS-422 Dick Steflik.
Intro to Ajax Fred Stluka Jan 25, 2006.
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
AJAX Chapters 19, 20.
Software Engineering for Internet Applications
Presentation transcript:

Asynchronous HTTP request generation in JavaScript (AJAX)

A motivating example Suppose we have a table which was created like this create table cs4408demo (name varchar(60), url varchar(255)); Suppose we have a utility for adding entries to this table It might be implemented as on the next slide

Add Organization utility <?php if ( ($name) && ($url) ) { $db = mysql_connect("localhost","myUserName","myPassword"); mysql_select_db("stories",$db); $query = "select * from cs4408demo where name='$name'"; $result=mysql_query($query,$db); if ($row=mysql_fetch_array($result)) { echo "Organization already in database"; } else { $query = "insert into cs4408demo (name,url) values ('$name','$url')"; mysql_query($query,$db); echo "Data stored"; } } else {?> "> Name of organization: URL of organization's website: Store details in database <?php } ?>

The form looks like this

A completed form

If the organization is new, the data are stored in the database

If the organization is old, the data are rejected

When is the check made? In the above scenario, the user must fill in all the data on the form and submit the form before he learns that he is wasting his time

When would it be better to make the check? It would be better if the user could learn as soon as possible that he is duplicating data that are already in the database It would be better if could learn this while he is still filling in the form That is, we can the browser to check the database while the user is still filling in the form

Javascript HTTP Request objects Javascript provides an object class for making HTTP requests Unfortunately, it has two different names: –In Microsoft browsers it is implemented as an ActiveXObject class called Microsoft.XMLHTTP –In other browsers it is a Javascript object class called XMLHttpRequest()

What good are these Javascript HTTP objects? Suppose we had a separate PHP program which could be called to see if an organization is already in the database We could use a Javascript HTTP Request object to call this program –immediately after the user has filled in the organization's name on the form –and while he is still filling the rest of the form If the response to this request says the organization is already present, he could be warned alerted before he submits the form

The checkOrg.php program <?php $db = mysql_connect("localhost","myUserName","`myPassword"); mysql_select_db("stories",$db); $query = "select * from cs4408demo where name='$name'"; $result=mysql_query($query,$db); if ($row=mysql_fetch_array($result)) { echo "Organization already in database"; } else { echo "Organization is new"; } ?>

Example call

Another example call

We could use a Javascript HTTP Request object to call this program Then we could use a Javascript string search to see if the source code for the response page contains the word already or the word new If the response page contains the word already, we could alert the user that he is duplicating existing data

Example duplication warning

Improved utility for adding organizations to database It is here:

First part of program is unchanged Add Organization utility <?php if ( ($name) && ($url) ) { $db = mysql_connect("localhost","myUserName","myPassword"); mysql_select_db("stories",$db); $query = "select * from cs4408demo where name='$name'"; $result=mysql_query($query,$db); if ($row=mysql_fetch_array($result)) { echo "Organization already in database"; } else { $query = "insert into cs4408demo (name,url) values ('$name','$url')"; mysql_query($query,$db); echo "Data stored"; } }

Second part of program is changed else {?> "> Name of organization: URL of organization's website: Store details in database... This will be defined on next few slides... <?php } ?>

Second part of program (contd.) function check(name) { if (window.XMLHttpRequest) // Mozilla, Safari,... { http_request = new XMLHttpRequest(); } else if (window.ActiveXObject) // IE { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } http_request.onreadystatechange = handleResponse; url=' http_request.open('GET', url,true); http_request.send(null); } function handleResponse() { if (http_request.readyState == 4) //response received { response=http_request.responseText; if (response.search('already') != -1) { alert('Organization already in database'); } } }

Second part of program (contd.) function check(name) { if (window.XMLHttpRequest) // Mozilla, Safari,... { http_request = new XMLHttpRequest(); } else if (window.ActiveXObject) // IE { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } http_request.onreadystatechange = handleResponse; url=' http_request.open('GET', url,true); http_request.send(null); } function handleResponse() { if (http_request.readyState == 4) //response received { response=http_request.responseText; if (response.search('already') != -1) { alert('Organization already in database'); } } }

AJAX methods abort() Stops the current requestgetAllResponseHeaders() Returns complete set of headers (labels and values) as a string getResponseHeader("headerLabel") Returns the string value of a single header label open("method", "URL"[, asyncFlag[, "userName"[, "password"]]]) Assigns destination URL, method, and other optional attributes of a pending request send(content) Transmits the request, optionally with postable string or DOM object data setRequestHeader("label", "value") Assigns a label/value pair to the header to be sent with a request

AJAX Properties onreadystatechange Event handler for an event that fires at every state change readyState Object status integer: 0 = uninitialized 1 = loading 2 = loaded 3 = interactive 4 = complete responseText String version of data returned from server process response XMLDOM-compatible document object of data returned from server process status Numeric code returned by server, such as 404 for "Not Found" or 200 for "OK" statusText String message accompanying the status code

A "bug" in AJAX AJAX implementations appear to be over- eager to cache responses to requests In most cases, this does not matter In those cases where it causes a problem, this over-eager caching can be overcome by inserting a time-stamp into each request Example on next page

Preventing over-eager caching Suppose the JavaScript variable url already contains the web address and data we want to use in our request We can prevent caching problems by making each request unique, by giving each request a time-stamp Here is an example: now = new Date(); url = url+'&time='+now.getTime(); http_request.open('GET', url,true); http_request.send(null);