Amazon Class ajax part1



Download 36.5 Kb.
Date02.02.2017
Size36.5 Kb.
#16293
Amazon Class AJAX Part1
Reference: https://developer.mozilla.org/en/AJAX/Getting_Started

AJAX was a designed to allow the client to send data to the server without having to have the rest the page be reloaded by the browser. Imagine if you were playing a video game and you had to have the whole screen refresh every time you clicked a mouse. That would make it unusable no matter how fast your computer was.


Circa 1998, asynchronous programming was starting to take hold inside companies who knew what they were doing. Google was building event driven C++ code( async C++ templates is described here: http://www.scs.stanford.edu/06wi-cs240d/sched/readings/sfs.pdf ) for GFS and MSFT introduced async programming in the server to the web browser in Internet Explorer 5.5. This allowed the browser to make a request to the server by submitting a callback function. This was not seen before in browser client side code. This worked beautifully in the world of slow network connections where the user population was dominated by AOL users trying to download content over slow POTS lines.
MSFT created AJAX by introducing a new JS object called XMLHTTPRequest object. Another initiative circa 1998 was the prediction XML would be the lingua franca between computers and would be sending huge volumes of XML data between them as a result of Internet connectivity.
In reality, very few applications now use XML to send data between a browser and server. AJAX should really be called AJAJ (Asynchronous Javascript JSON) since most traffic between Servers and browsers is formatted in JSON or in our examples in simple text.
To implement AJAX you need 4 things:

  1. Create an XMLHTTPRequest

var requestObj = new XMLHTTPRequest();

var requestObj = new ActiveXObject(“Microsoft.XMLHTTP”);


combine the 2 by testing for the functionality as:

var requestObj;

if(window.XMLHTTPRequest){

requestObj = new XMLHTTPRequest();

}else if(window.ActiveXObject){

requestObj = new ActiveXObject(“Microsoft.XMLHTTP”);

}



  1. Add a callback function to the XMLHTTPRequest object and send the AJAX Request. This callback function is scheduled for execution when the response from the server reaches the browser.

requestObj.onreadystatechange = handleResponse;

requestObj.onreadystatechange = handleResponse;

requestObj.open('GET', weatherURL, true);

requestObj.send(null);
Set up the parameters for the request to the server, this includes the URL and any encoded parameters. A url is called a fat url when it contains question marks indicating name/value pairs are in the header. Send the request to the sender.


  1. Write the server method to accept the browser request, decode it and send back a response.

In our server method we will use a JavaServlet which will implement a doGet method if we specify GET in the XMLHttpRequest or a doPost method if we choose POST. Note we used GET above:



  1. Decode the response from the Server. The data is inside XMLHttpResponse,

Here is the full code for an WebPage implementing an AJAX call to a Yahoo Weather Server:



Download 36.5 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page