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