1 / 22

CNIT 133 Interactive Web Pags – JavaScript and AJAX

CNIT 133 Interactive Web Pags – JavaScript and AJAX. AJAX. Agenda. My Web Site: http://fog.ccsf.edu/~hyip (download syllabus, class notes). HTTP XMLHttpRequest XMLHttpRequest properties XMLHttpRequest methods AJAX tutorial AJAX sample. Scripting HTTP.

pbruggeman
Download Presentation

CNIT 133 Interactive Web Pags – JavaScript and AJAX

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CNIT 133 Interactive Web Pags –JavaScript and AJAX AJAX

  2. Agenda • My Web Site: http://fog.ccsf.edu/~hyip (download syllabus, class notes). • HTTP • XMLHttpRequest • XMLHttpRequest properties • XMLHttpRequest methods • AJAX tutorial • AJAX sample

  3. Scripting HTTP • The Hypertext Transfer Protocol (HTTP) specifies how web browsers request document from and post form contents to web servers, and how web servers respond to those requests and posts. • HTTP requests can be initiated when a script sets the location property of a window object or calls the submit() method of a form object. • In both cases, the browser loads a new page into the window, overwriting any script that was running there. • AJAX, however, can be considered as how JavaScript code can communicate with a web server without causing the web browser to reload the currently displayed page.

  4. Using XMLHttpRequest • Scripting HTTP with XMLHttpRequest is a three-part process: • Creating an XMLHttpRequest object • Specifying and submitting your HTTP request to a web server • Synchronously or asynchronously retrieving the server’s response

  5. Obtaining a Request Object • The XMLHttpRequest object has never been standardized, and the process of creating one is different in Internet Explorer than on other platforms. • In most browsers, you create an XMLHttpRequest object with a simple constructor call: var request = new XMLHttpRequest(); • Prior to Internet Explorer 7, IE does not have a native XMLHttpRequest() constructor function. In IE 5 and 6, XMLHttpRequest is an ActiveX object: var request = new ActiveXObject(“Msxml2.XMLHTTP”); • The name of the object is different in different releases of Microsoft’s XML.HTTP library. You may sometimes have to use this code: var request = new ActiveXObject(“Microsoft.XMLHTTP”);

  6. Properties of the request object • The XMLHttpRequest Object contains the following important properties: • The onreadystatechange property contains function that will process the response from a server. • The readyState property contains the status of the server’s response. ( 0 to 4) • The status property contains the status of the web page. (200=ok; 404=page not found) • The responseText property contains the data sent back from the server in string format. • The responseXML property contains the data sent back from the server in XML format. • NOTE: Data will be either populated in responseText or responseXML property, but not both.

  7. Methods of the request object • The XMLHttpRequest object contains two important methods: • open() • send()

  8. Submitting a Request • Once an XMLHttpRequest object has been created and properties of the XMLHttpRequest has been populated, the next step is to submit a request to a web server. • First call the open() method to specify the URL you are requesting and the HTTP method of the request: var request = new XMLHttpRequest(); request.open(method, url, async); // method = "GET“/"POST“/"HEAD" async = true(default)/false request.open("GET", url, true); • Second, set any necessary request headers: request.setRequestHeader("User-Agent", "XMLHttpRequest"); request.setRequestHeader("Accept-Language", "en"); request.setRequestHeader("If-Modified-Since", lastRequestTime.toString()); • Third, send the request to the server: request.send(null);

  9. Handling an Asynchronous Response • To use an XMLHttpRequest object in asynchronous mode, pass true as the third argument to the open() method or simply omit the third argument. • The send() method sends the request to the server and then returns immediately. • For XMLHttpRequest, the event handler is set on the onreadystatechange property. • The event-handler function is invoked whenever the value of the readystate property changes. • NOTE: the XMLHttpRequest object has never been standardized, and browsers differ in their handling of readystate 3. Therefore, it is safest to ignore any value of readystate other thatn 4 (The server’s response is complete).

  10. AJAX Tutorial • AJAX stands for Asynchronous JavaScript And XML. • AJAX is not a new programming language, but a technique for creating better, faster, and more interactive web applications. • With AJAX, your JavaScript can communicate directly with the server, using the JavaScript XMLHttpRequest object. With this object, your JavaScript can trade data with a web server, without reloading the page. • AJAX uses asynchronous data transfer (HTTP requests) between the browser and the web server, allowing web pages to request small bits of information from the server instead of whole pages. • The AJAX technique makes Internet applications smaller, faster and more user-friendly. • AJAX is a browser technology independent of web server software.

  11. AJAX Introduction • What You Should Already Know • Before you continue you should have a basic understanding of the following: • HTML / XHTML • JavaScript

  12. AJAX is Based on Web Standards • AJAX is based on the following web standards: • JavaScript • XML • HTML • CSS • The web standards used in AJAX are well defined, and supported by all major browsers. AJAX applications are browser and platform independent.

  13. AJAX Uses HTTP Requests • In traditional JavaScript coding, if you want to get any information from a database or a file on the server, or send user information to a server, you will have to make an HTML form and GET or POST data to the server. The user will have to click the "Submit" button to send/get the information, wait for the server to respond, then a new page will load with the results. • Because the server returns a new page each time the user submits input, traditional web applications can run slowly and tend to be less user-friendly. • With AJAX, your JavaScript communicates directly with the server, through the JavaScript XMLHttpRequest object • With an HTTP request, a web page can make a request to, and get a response from a web server - without reloading the page. The user will stay on the same page, and he or she will not notice that scripts request pages, or send data to a server in the background.

  14. The XMLHttpRequest Object • By using the XMLHttpRequest object, a web developer can update a page with data from the server after the page has loaded! • AJAX was made popular in 2005 by Google (with Google Suggest). • Google Suggest is using the XMLHttpRequest object to create a very dynamic web interface: When you start typing in Google's search box, a JavaScript sends the letters off to a server and the server returns a list of suggestions. (http://www.google.com/webhp?complete=1&hl=en) • The XMLHttpRequest object is supported in Internet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Opera 8+, and Netscape 7.

  15. More About the XMLHttpRequest Object • Before sending data to the server, we have to explain three important properties of the XMLHttpRequest object. 1. The onreadystatechange Property • After a request to the server, we need a function that can receive the data that is returned by the server. • The onreadystatechange property stores the function that will process the response from a server. The following code defines an empty function and sets the onreadystatechange property at the same time: xmlHttp.onreadystatechange=function() { // We are going to write some code here }

  16. More About the XMLHttpRequest Object 2. The readyState Property • The readyState property holds the status of the server's response. Each time the readyState changes, the onreadystatechange function will be executed. • Here are the possible values for the readyState property: State Description 0 The request is not initialized 1 The request has been set up 2 The request has been sent 3 The request is in process 4 The request is complete

  17. More About the XMLHttpRequest Object • We are going to add an If statement to the onreadystatechange function to test if our response is complete (this means that we can get our data): xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { // Get the data from the server's response } } 3. The responseText Property • The data sent back from the server can be retrieved with the responseText property. xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { document.getElementById("txt2").innerHTML=xmlHttp.responseText; } }

  18. Sending a Request to the Server • To send off a request to the server, we use the open() method and the send() method. • The open() method takes three arguments. The first argument defines which method to use when sending the request (GET or POST). The second argument specifies the URL of the server-side script. The third argument specifies that the request should be handled asynchronously. The send() method sends the request off to the server. If we assume that the HTML and PHP file are in the same directory, the code would be: xmlHttp.open("GET",“ajax_sample.php",true); xmlHttp.send(null);

  19. AJAX sample <html> <head> <script> var xmlHttp function send() { var str = document.getElementById("txt1").value; if (str.length==0) { document.getElementById("txt2").innerHTML=""; alert("No Data send"); return; } xmlHttp=GetXmlHttpObject(); if (xmlHttp==null) { alert ("Your browser does not support AJAX!"); return; }

  20. AJAX sample var url="../../ajax_sample.php"; url=url+"?q="+str; url=url+"&sid="+Math.random(); xmlHttp.onreadystatechange=stateChanged; xmlHttp.open("GET",url,true); xmlHttp.send(null); } function stateChanged() { if (xmlHttp.readyState==4) { if (xmlHttp.status == 200) { document.getElementById("txt2").innerHTML=xmlHttp.responseText; } else { alert("Problem with retrieving data, status is : " + xmlHttp.statusText); } } }

  21. AJAX sample function GetXmlHttpObject() { var loc_xmlHttp=null; try { // Firefox, Opera 8.0+, Safari, IE 7 loc_xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer 5 or 6 try { loc_xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } // Older version of IE catch (e) { loc_xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return loc_xmlHttp; }

  22. AJAX sample </script> </head> <body> <h1>Ajax sample, return text</h1> <form> Data will be sent to PHP: <input type="text" id="txt1"> <br> <input type="button" onclick="send()" value="S E N D"> </form> <p>Data Returned from PHP: <span id="txt2"></span></p> </body> </html>

More Related