Ajax Flashcards
What does AJAX stand for?
Asynchronous JavaScript and XML
Why Web Apps?
Everyone has a browser
Automatic updates when accessed in browser
Downside of Web Apps
GUI is poor. HTML is OK for static documents but rubbish for programs
HTTP is a poor protocol for communication
Why AJAX?
Instant updates
Asynchronous results
More widgets than GUI elements
What is the basic ajax process?
JAVASCRIPT:
- Define a Request Object
- Initiate Request (with GET/ POST)
- Handle Response
HTML
- Load JavaScript
- Designate control that initiates request
- Give IDs to input elements, and to output region.
Ajax Process:
JavaScript:
Code for 1. Define a Request Object
function getRequestObject(){ if(window.XMLHttpRequest){ return (new XMLHttpRequest()); }else }return(null); }
Ajax Process:
JavaScript
Code for 2. Initiating a request
function initiateRequest(resultsRegion){ var req = getRequestObject(); req.onreadystatechange = function() {handleResponse(req, resultsRegion)}; req.open("GET", "myFile.html", true); req.send(null); } }
Ajax Process:
JavaScript:
Code for 3. Handle Response
function handleResponse(req, resultsRegion){ if(req.readyState == 4) && (req.Status == 200){ htmlInsert(resultsRegion, req.responseText); } }
True or false; JavaScript lets you pass functions around?
True
As in Ajax Process, JS, (2) Initiating a Request: reques.onreadystatechange = handleResponse;
Describe JavaScript first class functions
JS lets you pass around functions, ie access them in another function:
request.onreadystatechange = handleResponse;
The code from the Initiate Request (2) function has a onreadystatechange that equals another function; the handleResponse (3) function.
JavaScript allows anonymous functions
var request = getRequestObject();
request.onreadystatechange = function() {handleResponse(request)}
By calling and putting the handleResponse function within a function(){handleReponse(request)}; you make the function anonymous rather than just goin request.ORSC = handleResponse;
Technologies needed in place to use Ajax?
Sever-side language such as JSP, .NET or PHP. JavaScript Web Browser Servlets Server, tomcat?
What is the correct way to use anonymous functions?
Use an anonymous function with a local copy of the request object embedded in the code ie for the 2. Initiate Request part of of the Ajax process:
function sendRequest(){ request = getRequestObject(); request.onreadystatechange = function() {handleResponse(request); }; }
What are the design deficiencies with using HTML to acheive Ajax (entProg/wk3)
Content is same on each request (HTML page doesnt change dynamically)
A better way to do this is with a JSP page.
HMTL page url is hardcoded into ajax-utils.js - instead make a generic function (address) then pass the page in from the HTML, so it can be used again with out areas.
Design deficiencies with using JSP pages to achieve Ajax (entProg/wk3)
The URL stays the same even when the output changes
If the browser caches the page, it returns wrong time for example - SolutionL cache-control and pragma headers
JSP is wrong tech if using Java - JSP is best for lots of HTML and little but no logic/java - Solution: use Servlet.
Design deficiencies with Servlet way of achieving Ajax (entProg/wk3)
Alerts are usually reserved for warnings, users prefer results in a page
Solution: use dynamic HTML to update page
Name 3 styles of Ajax
Content-centric Ajax: Server sends exact HTML to be displayed. Client simply inserts content into these tables. Not good if the server is going to be used for different things. May only want to send data:
Data-centric Ajax:
The server sends raw data
The client parses the data and builds HTML
Script-centric Ajax:
Server sends JavaScript functions
Client executes functions
Inflexible - requires server to know too much about client
What advantage does Ajax have over standard client server apps using HTML/HTTP
Communicate with server without leaving the page
Frequent small updates - faster
Data is kept on screen - no refresh
Ajax allows more like desktop apps
Advantages and disadvantages of XML
Advantages - Human readable, structured metadata
Disadvantages - Verbose, larger to procress
Advantages and disadvantages of JSON
Advantages - lightweight (serialisation), more efficient than XML, mapped straight to objects.
Disadvantages - not easily read by humans. No error handling. Security issues with data transfer
Advantages and disadvantages of plain text
Advantage: extremely lightweight, simple to process
Disadvantage: only basic data, no standard way to represent binary data, problems to SQL
Ajax Process:
JavaScript:
Code for 3. htmlInsert
function htmlInsert(id, htmlData){ document.getElementById(id).innerHTML = htmlData; }
HTML code for ajax
<div></div>