DOC PREVIEW
Penn CIT 597 - Ajax Lecture notes

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

AjaxThe hypeThe realityHow Ajax worksUnderlying technologiesStarting from the browser…The XMLHttpRequest objectGetting the XMLHttpRequest objectPreparing the XMLHttpRequest objectSending the XMLHttpRequest objectThe escape methodPutting it all togetherOn the server sideGetting the responseThe magic number 4The magic number 200Using response dataDisplaying the responseThe readyState propertySummaryProblem: Lost responseProblem: Only works the first timeProblem: JavaScript isn’t loadedBack to the HTML DOMAdding and removing event handlers<div> and <span> againinnerHTMLThe EndJan 14, 2019AjaxThe hypeAjax (sometimes capitalized as AJAX) stands for Asynchronous JavaScript And XMLAjax is a technique for creating “better, faster, more responsive web applications”Web applications with Ajax are supposed to replace all our traditional desktop applicationsThese changes are so sweeping that the Ajax-enabled web is sometimes know as “Web 2.0”Ajax also cures cancer, ends hunger, and brings about world peaceThe realityAjax is a technique for creating “better, faster, more responsive web applications”But they still aren’t as responsive as desktop applications, and probably never will beWeb applications are useless when you aren’t on the webGUIs are HTML forms (and you know how beautiful and flexible those are)The technology has been available for some timeNevertheless, Ajax is the “hot new thing” because:Google uses it extensively, in things like Google Earth and Google SuggestIt has been given a catchy nameAjax is a useful technology, and a good thing to have on your resuméHow Ajax worksYou do something with an HTML form in your browserJavaScript on the HTML page sends an HTTP request to the serverThe server responds with a small amount of data, rather than a complete web pageJavaScript uses this data to modify the pageThis is faster because less data is transmitted and because the browser has less work to doUnderlying technologiesJavaScriptHTMLCSSXMLXML is often used for receiving data from the serverPlain text can also be used, so XML is optionalHTTPAll these are open standardsStarting from the browser…Your browser must allow JavaScript, or Ajax won’t workOtherwise, there’s nothing special required of the browserYour browser has some some way of providing data to the server—usually from an HTML formJavaScript has to handle events from the form, create an XMLHttpRequest object, and send it (via HTTP) to the serverNothing special is required of the server—every server can handle HTTP requestsDespite the name, the XMLHttpRequest object does not require XMLThe XMLHttpRequest objectJavaScript has to create an XMLHttpRequest objectFor historical reasons, there are three ways of doing thisFor most browsers, just dovar request = new XMLHttpRequest();For some versions of Internet Explorer, dovar request = new ActiveXObject("Microsoft.XMLHTTP");For other versions of Internet Explorer, dovar request = new ActiveXObject("Msxml2.XMLHTTP");Doing it incorrectly will cause an ExceptionThe next slide shows a JavaScript function for choosing the right way to create an XMLHttpRequest objectGetting the XMLHttpRequest objectvar request = null; // we want this to be globalfunction getXMLHttpRequest( ) { try { request = new XMLHttpRequest(); } catch(err1) { try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch(err2) { try { request = new ActiveXObject("Msxml2.XMLHTTP"); } catch(err3) { request = null; } } } if (request == null) alert("Error creating request object!");}Preparing the XMLHttpRequest objectOnce you have an XMLHttpRequest object, you have to prepare it with the open methodrequest.open(method, URL, asynchronous)The method is usually 'GET' or 'POST'The URL is where you are sending the dataIf using a 'GET', append the data to the URLIf using a 'POST', add the data in a later stepIf asynchronous is true, the browser does not wait for a response (this is what you usually want)request.open(method, URL)As above, with asynchronous defaulting to trueSending the XMLHttpRequest objectOnce the XMLHttpRequest object has been prepared, you have to send itrequest.send(null);This is the version you use with a GET requestrequest.send(content);This is the version you use with a POST requestThe content has the same syntax as the suffix to a GET requestPOST requests are used less frequently than GET requestsFor POST, you must set the content type:request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');request.send('var1=' + value1 + '&var2=' + value2);The escape methodIn the previous slide we constructed our parameter list to the serverrequest.send('var1=' + value1 + '&var2=' + value2);This list will be appended to the URL (for a GET)However, some characters are not legal in a URLFor example, spaces should be replaced by %20The escape method does these replacements for yourequest.send('var1=' + escape(value1) + '&var2=' + escape(value2));Putting it all togetherHead:function getXMLHttpRequest () { ... } // from an earlier slidefunction sendRequest() { getXMLHttpRequest(); var url = some URL request.open("GET", url, true); // or POST request.onreadystatechange = handleTheResponse; request.send(null); // or send(content), if POSTfunction handleTheResponse() { if (request.readyState == 4) { if (request.status == 200) { var response = request.responseText; // do something with the response string } else { alert("Error! Request status is " + request.status); } }}Body: <input value="Click Me" type="button" onclick="sendRequest">On the server sideThe server gets a completely standard HTTP requestIn a servlet, this would go to a doGet or doPost methodThe response is a completely standard HTTP response, but……Instead of returning a complete HTML page as a response, the server returns an arbitrary text string (possibly XML, possibly something else)Getting the responseAjax uses asynchronous calls—you don’t wait for the responseInstead, you have to handle an eventrequest.onreadystatechange = someFunction;This is a function assignment, not a function callHence, there are no


View Full Document

Penn CIT 597 - Ajax Lecture notes

Documents in this Course
DOM

DOM

21 pages

More DOM

More DOM

11 pages

Rails

Rails

33 pages

DOM

DOM

21 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

Rake

Rake

12 pages

Ruby

Ruby

58 pages

DOM

DOM

21 pages

Tomcat

Tomcat

16 pages

DOM

DOM

21 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Html

Html

27 pages

DOM

DOM

22 pages

RELAX NG

RELAX NG

30 pages

Servlets

Servlets

28 pages

XHTML

XHTML

13 pages

DOM

DOM

21 pages

DOM

DOM

21 pages

Servlets

Servlets

26 pages

More CSS

More CSS

18 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Load more
Download Ajax Lecture notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Ajax Lecture notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Ajax Lecture notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?