0
Ajax, JavaScript and PHP <ul><li>Chapter 20 – Scripting HTTP (JS Concept) </li></ul><ul><li>AJAX - http://www.w3schools.co...
Scripting HTTP <ul><li>The Hypertext Transfer Protocol (HTTP) specifies how web browsers request document from and post fo...
Using XMLHttpRequest <ul><li>Scripting HTTP with XMLHttpRequest is a three-part process: </li></ul><ul><ul><li>Creating an...
Obtaining a Request Object <ul><li>The XMLHttpRequest object has never been standardized, and the process of creating one ...
Submitting a Request <ul><li>Once an XMLHttpRequest object has been created, the next step is to submit a request to a web...
Obtaining a Synchronous Response <ul><li>Refer to Page 482 (20.1.3) </li></ul>
Handling an Asynchronous Response <ul><li>To use an XMLHttpRequest object in asynchronous mode, pass true as the third arg...
AJAX Tutorial <ul><li>AJAX stands for  A synchronous  J avaScript  A nd  X ML. </li></ul><ul><li>AJAX is a type of program...
AJAX Introduction <ul><li>AJAX stands for Asynchronous JavaScript And XML.  </li></ul><ul><li>What You Should Already Know...
Asynchronous JavaScript and XML <ul><li>AJAX = Asynchronous JavaScript and XML </li></ul><ul><li>AJAX is not a new program...
AJAX is Based on Web Standards <ul><li>AJAX is based on the following web standards: </li></ul><ul><ul><li>JavaScript   </...
AJAX Uses HTTP Requests <ul><li>In traditional JavaScript coding, if you want to get any information from a database or a ...
The XMLHttpRequest Object <ul><li>By using the XMLHttpRequest object, a web developer can update a page with data from the...
AJAX - Browser Support <ul><li>The keystone of AJAX is the XMLHttpRequest object. </li></ul><ul><li>Different browsers use...
AJAX - More About the XMLHttpRequest Object <ul><li>Before sending data to the server, we have to explain three important ...
AJAX - More About the XMLHttpRequest Object <ul><ul><li>We are going to add an If statement to the onreadystatechange func...
AJAX - Sending a Request to the Server  <ul><li>To send off a request to the server, we use the open() method and the send...
AJAX - The Server-Side ASP Script  <ul><li>Now we are going to create the script that displays the current server time. </...
AJAX Example <ul><li>See ajaxhelp.html </li></ul>
Upcoming SlideShare
Loading in...5
×

Ajax, JavaScript and PHP Chapter 20 – Scripting HTTP (JS Concept)

1,824

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,824
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
81
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Ajax, JavaScript and PHP Chapter 20 – Scripting HTTP (JS Concept)"

  1. 1. Ajax, JavaScript and PHP <ul><li>Chapter 20 – Scripting HTTP (JS Concept) </li></ul><ul><li>AJAX - http://www.w3schools.com/ajax/ </li></ul>
  2. 2. Scripting HTTP <ul><li>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. </li></ul><ul><li>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. </li></ul><ul><li>In both cases, the browser loads a new page into the window, overwriting any script that was running there. </li></ul><ul><li>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. </li></ul>
  3. 3. Using XMLHttpRequest <ul><li>Scripting HTTP with XMLHttpRequest is a three-part process: </li></ul><ul><ul><li>Creating an XMLHttpRequest object </li></ul></ul><ul><ul><li>Specifying and submitting your HTTP request to a web server </li></ul></ul><ul><ul><li>Synchronously or asynchronously retrieving the server’s response </li></ul></ul>
  4. 4. Obtaining a Request Object <ul><li>The XMLHttpRequest object has never been standardized, and the process of creating one is different in Internet Explorer than on other platforms. </li></ul><ul><li>In most browsers, you create an XMLHttpRequest object with a simple constructor call: </li></ul><ul><ul><li>var request = new XMLHttpRequest(); </li></ul></ul><ul><li>Prior to Internet Explorer 7, IE does not have a native XMLHttpRequest() constructor function. In IE 5 and 6, XMLHttpRequest is an ActiveX object: </li></ul><ul><ul><ul><li>var request = new ActiveXObject(“Msxml2.XMLHTTP”); </li></ul></ul></ul><ul><li>The name of the object is different in different releases of Microsoft’s XML.HTTP library. You may sometimes have to use this code: </li></ul><ul><ul><li>var request = new ActiveXObject(“Microsoft.XMLHTTP”); </li></ul></ul>
  5. 5. Submitting a Request <ul><li>Once an XMLHttpRequest object has been created, the next step is to submit a request to a web server. </li></ul><ul><li>First call the open() method to specify the URL you are requesting and the HTTP method of the request: </li></ul><ul><ul><li>var request = new XMLHttpRequest(); </li></ul></ul><ul><ul><li>request.open(method, url, async); </li></ul></ul><ul><ul><li>// method = “GET”/”POST”/”HEAD” async = true(default)/false </li></ul></ul><ul><ul><li>request.open(“GET”, url, true); </li></ul></ul><ul><li>Second, set any necessary request headers: </li></ul><ul><ul><li>request.setRequestHeader(“User-Agent”, “XMLHttpRequest”); </li></ul></ul><ul><ul><li>request.setRequestHeader(“Accept-Language”,”en”); </li></ul></ul><ul><ul><li>request.setRequestHeader(“If-Modified-Since”, lastRequestTime.toString()); </li></ul></ul><ul><li>Third, send the request to the server: </li></ul><ul><ul><li>request.send(null); </li></ul></ul>
  6. 6. Obtaining a Synchronous Response <ul><li>Refer to Page 482 (20.1.3) </li></ul>
  7. 7. Handling an Asynchronous Response <ul><li>To use an XMLHttpRequest object in asynchronous mode, pass true as the third argument to the open() method or simply omit the third argument. </li></ul><ul><li>The send() method sends the request to the server and then returns immediately. </li></ul><ul><li>For XMLHttpRequest, the event handler is set on the onreadystatechange property. </li></ul><ul><li>The event-handler function is invoked whenever the value of the readystate property changes. </li></ul><ul><li>See Page 483 Table 20-1 </li></ul><ul><li>NOTE: the XMLHttpRequest object has never been standardized, and browsers differ in their handling of readstate 3. Therefore, it is safest to ignore any value of readystate other thatn 4 (The server’s response is complete). </li></ul>
  8. 8. AJAX Tutorial <ul><li>AJAX stands for A synchronous J avaScript A nd X ML. </li></ul><ul><li>AJAX is a type of programming made popular in 2005 by Google (with Google Suggest). </li></ul><ul><li>AJAX is not a new programming language, but a new way to use existing standards. </li></ul><ul><li>With AJAX you can create better, faster, and more user-friendly web applications. </li></ul><ul><li>AJAX is based on JavaScript and HTTP requests. </li></ul>
  9. 9. AJAX Introduction <ul><li>AJAX stands for Asynchronous JavaScript And XML. </li></ul><ul><li>What You Should Already Know </li></ul><ul><ul><li>Before you continue you should have a basic understanding of the following: </li></ul></ul><ul><ul><ul><li>HTML / XHTML </li></ul></ul></ul><ul><ul><ul><li>JavaScript </li></ul></ul></ul>
  10. 10. Asynchronous JavaScript and XML <ul><li>AJAX = Asynchronous JavaScript and XML </li></ul><ul><li>AJAX is not a new programming language, but a technique for creating better, faster, and more interactive web applications. </li></ul><ul><li>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. </li></ul><ul><li>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. </li></ul><ul><li>The AJAX technique makes Internet applications smaller, faster and more user-friendly. </li></ul><ul><li>AJAX is a browser technology independent of web server software. </li></ul>
  11. 11. AJAX is Based on Web Standards <ul><li>AJAX is based on the following web standards: </li></ul><ul><ul><li>JavaScript </li></ul></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>HTML </li></ul></ul><ul><ul><li>CSS </li></ul></ul><ul><li>The web standards used in AJAX are well defined, and supported by all major browsers. AJAX applications are browser and platform independent. </li></ul>
  12. 12. AJAX Uses HTTP Requests <ul><li>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 &quot;Submit&quot; button to send/get the information, wait for the server to respond, then a new page will load with the results. </li></ul><ul><li>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. </li></ul><ul><li>With AJAX, your JavaScript communicates directly with the server, through the JavaScript XMLHttpRequest object </li></ul><ul><li>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. </li></ul>
  13. 13. The XMLHttpRequest Object <ul><li>By using the XMLHttpRequest object, a web developer can update a page with data from the server after the page has loaded! </li></ul><ul><li>AJAX was made popular in 2005 by Google (with Google Suggest). </li></ul><ul><li>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. </li></ul><ul><li>The XMLHttpRequest object is supported in Internet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Opera 8+, and Netscape 7. </li></ul>
  14. 14. AJAX - Browser Support <ul><li>The keystone of AJAX is the XMLHttpRequest object. </li></ul><ul><li>Different browsers use different methods to create the XMLHttpRequest object. </li></ul><ul><li>Internet Explorer uses an ActiveXObject , while other browsers uses the built-in JavaScript object called XMLHttpRequest . </li></ul>
  15. 15. AJAX - More About the XMLHttpRequest Object <ul><li>Before sending data to the server, we have to explain three important properties of the XMLHttpRequest object. </li></ul><ul><li>The onreadystatechange Property </li></ul><ul><ul><li>After a request to the server, we need a function that can receive the data that is returned by the server. </li></ul></ul><ul><ul><li>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: </li></ul></ul><ul><ul><li>xmlHttp.onreadystatechange=function() </li></ul></ul><ul><ul><li> { </li></ul></ul><ul><ul><li> // We are going to write some code here </li></ul></ul><ul><ul><li> } </li></ul></ul><ul><li>The readyState Property </li></ul><ul><ul><li>The readyState property holds the status of the server's response. Each time the readyState changes, the onreadystatechange function will be executed. </li></ul></ul><ul><ul><li>Here are the possible values for the readyState property: </li></ul></ul><ul><ul><ul><li>State Description </li></ul></ul></ul><ul><ul><ul><li>0 The request is not initialized </li></ul></ul></ul><ul><ul><ul><li>1 The request has been set up </li></ul></ul></ul><ul><ul><ul><li>2 The request has been sent </li></ul></ul></ul><ul><ul><ul><li>3 The request is in process </li></ul></ul></ul><ul><ul><ul><li>4 The request is complete </li></ul></ul></ul>
  16. 16. AJAX - More About the XMLHttpRequest Object <ul><ul><li>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): </li></ul></ul><ul><ul><ul><li>xmlHttp.onreadystatechange=function() { </li></ul></ul></ul><ul><ul><ul><li>if(xmlHttp.readyState==4) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>// Get the data from the server's response } </li></ul></ul></ul><ul><ul><ul><li> } </li></ul></ul></ul><ul><li>The responseText Property </li></ul><ul><ul><li>The data sent back from the server can be retrieved with the responseText property. </li></ul></ul><ul><ul><li>In our code, we will set the value of our &quot;time&quot; input field equal to responseText: </li></ul></ul><ul><ul><ul><li>xmlHttp.onreadystatechange=function() { </li></ul></ul></ul><ul><ul><ul><li>if(xmlHttp.readyState==4) { document.myForm.time.value=xmlHttp.responseText; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li> } </li></ul></ul></ul>
  17. 17. AJAX - Sending a Request to the Server <ul><li>To send off a request to the server, we use the open() method and the send() method. </li></ul><ul><li>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 ASP file are in the same directory, the code would be: </li></ul><ul><ul><ul><li>xmlHttp.open(&quot;GET&quot;,&quot;time.asp&quot;,true); </li></ul></ul></ul><ul><ul><ul><li>xmlHttp.send(null); </li></ul></ul></ul>
  18. 18. AJAX - The Server-Side ASP Script <ul><li>Now we are going to create the script that displays the current server time. </li></ul><ul><li>The responseText property (explained in the previous chapter) will store the data returned from the server. Here we want to send back the current time. The code in &quot;time.asp&quot; looks like this: </li></ul><ul><ul><ul><li><% </li></ul></ul></ul><ul><ul><ul><li>response.expires=-1 </li></ul></ul></ul><ul><ul><ul><li>response.write(time) </li></ul></ul></ul><ul><ul><ul><li>%> </li></ul></ul></ul><ul><li>Note: The Expires property sets how long (in minutes) a page will be cached on a browser before it expires. If a user returns to the same page before it expires, the cached version is displayed. Response.Expires=-1 indicates that the page will never be cached. </li></ul>
  19. 19. AJAX Example <ul><li>See ajaxhelp.html </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×