This is part 1 of my article series, Ajax Web Technology Explained. It is a two part series.
When you click a link on a web page a new web page should normally be produced at your browser. Now this web page is sent from the server. There are times when you need just a peace of text from the server and not a whole web page. Traditionally a whole web page would be sent with the small peace of text, you need. However, if just the small peace of text is sent, the time taken will be small compared to when the whole page is sent.
Ajax is a new technology, that enables you pull a bit of info (text) from the server and not a whole page. In this way you obtain the text information faster, than if you where to obtain it with a whole web page. Ajax can still be used to obtain large text, which does not have the HTML tags that form a web page. Ajax can be used to obtain (download) what is called XML files, but we shall not look into the case of XML files in this article.
Note: If you cannot see the code or if you think anything is missing (broken link), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what has been typed.
Example of Ajax Technology
www.google.com (Google Suggest) made Ajax popular in 2005. As you start typing letters in the Google Search box, Ajax at the client's browser sends the letters to the server, and the server returns a list of suggestions (words). You might have noticed this already, if you are used to the Internet.
What is Ajax?
Ajax is code that that web designer writes as he prepares his web page. AJAX is abbreviation for Asynchronous JavaScript And XML. You do not need to understand all that vocabulary in order to understand Ajax or this article. In order to understand this article (that is, Ajax), you need to have basic knowledge in HTML, basic knowledge in JavaScript and JavaScript objects, and a notion of database. The Ajax code is written in JavaScript in an HTML document.
Ajax is used to retrieve data (text) by a page at the client's browser to fit into that same page at the client or used by JavaScript in that same page.
With Ajax, just the text is retrieve from the server; the HTML elements that would have been added to the text to form a web page are not transmitted with the text.
The Ajax process is initiated by the client (user on his web page at browser); the process is executed without the user knowing; it is generally fast, so the user does not notice that information has been got from a server residing in some other country.
Advantages of Ajax
Traditionally, to retrieve data from the server, you have to fill a web page form, click the Submit button for the form to be sent to the server and then wait for the data to be included in a web page at the server and then sent back to the client. In some cases the page at the server would already be ready and then it would be sent by the server to the client. Here I give you the advantages of Ajax over this traditional method.
- With Ajax data is retrieved from the server faster than when it had to be retrieved as a web page.
- If processing of the retrieved data is done at the client, your processing time at the server is saved.
- The speed of interaction between browser and web server as a result of speed of retrieving data is increased.
Technical Aspects
Ajax consists of two code parts; one part is in JavaScript at the client and the other part is at the server. The part at the server can be in Perl, PHP or some other language. The part at the client, calls the part at the server. In this two part article series, I will explain only the part at the client's browser. What the part at the server has to do is to look for the data at the server and send in response to the part (request) at the client. The code at the server does not involve anything new. Different programs like Perl and PHP have their different ways of achieving this and so I cannot address that in this article (series).
Ajax at Client
At the client you write Ajax is JavaScript code. The code has to do the following:
- Open (start) a connection between the client computer and the server.
- Send the request for the information (text) the user needs.
- Wait for the response (this normally takes a short time) and receive it.
JavaScript has an object that can achieve all the above three objectives. Note that objects make programming easy. With objects like the one of interest, the difficult programming statements have already been written by someone else. You simply have to use the object. All you have to know here is how to use the object's properties and methods. These properties have already been declared for you; the methods have already been defined for you; you just have to know how to use them and use them. We shall learn how to use them and use them in this article.
The XMLHttpRequest Constructor
You, the web programmer is the one to create the JavaScript object that will carry out the above objectives. You do this by simply using the constructor function,
new XMLHttpRequest()
For example, if you want the object to be called myAjax, in your JavaScript, you would type:
var myAjax = new XMLHttpRequest();
Let us call this variable, the Ajax Object. Well, there is trouble; the above code works in Mozilla Firefox, Netscape and other major browsers except Internet Explorer. Internet Explorer versions would not produce the Ajax object using the above constructor function. These versions have their own constructor functions to produce the Ajax object. The different Internet Explorer versions can be grouped in three sets based on three different constructor functions they use to produce the Ajax object. One set produces the object as follows:
var myAjax = new XDomainRequest();
Another set produces the object as follows:
var myAjax = new ActiveXObject("Msxml2.XMLHTTP");
The last set produces the object as follows:
var myAjax = new ActiveXObject("Microsoft.XMLHTTP");
In order to write code that will work for major browsers, you need an "integrated" try-catch-block as the following code segment illustrates:
var myAjax;
try
{
// Firefox, Opera 8.0+, Safari
myAjax = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
myAjax = new XDomainRequest();
}
catch (e)
{
try
{
myAjax = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
myAjax = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
}
}
}
}
The first thing in the code segment above is the declaration of a variable, which the Ajax object created will be assigned to. As you can see, the first try-catch is for non-Internet Explorer browsers. The rest of the try-catches are for different versions (three sets) of Internet Explorer. If none of the tries work, then it means the user's browser is not a major browser and does not support Ajax; an alert box displays this.
After creating the Ajax object, the object has to achieve the above three objectives, which are repeated below:
- Open a connection between the client computer and the server.
- Send the request for the information (text) the user needs.
- Wait for the response and receive it.
The GET and POST methods
Before we look at the first objective above, we have to know that there are two methods by which a request can be sent by the Ajax object to the server. They are called the GET and the POST methods. Now, these methods are not the Ajax object methods. They are actually called HTTP methods. Well, you do not need to understand HTTP in order to understand this article (or Ajax - the topic).
The difference between the GET and POST methods is not in the meanings of the word GET and POST. It is in the way they are implemented (see below).
Opening a Connection
The Ajax object created has a method called the open() method. The simple syntax is
myAjax.open("HTTP Method", "URL", true);
myAjax is the Ajax object you created. It is followed by a dot and then the open() method. This method opens the connection between the client computer (browser) and the server.
The open() method has three parameters. The first one is either the word "GET" or "POST" in quotes. This is how you use either the GET method of HTTP or the POST method of HTTP. I will give you the advantage and disadvantage of the GET and POST method shortly. The second parameter is the URL of the file in the server that will look for the text and send to the client. That file at the server has to be an executable file. The third and last parameter must always be true, as shown above. You do not need the quotes around true.
An example of the above statement is:
myAjax.open("GET", "http://www.somewebsite.com/sendtext.php", true);
In this example, the executable file is, sendtext.php, a PHP file.
Well, the full syntax for the open() method is,
open(HTTP Method, URL, true, [username,] [password])
The user name and passwords are optional. These are used when the site at the server is protected.
Sending the Request
After you open the connection, with a statement like the last but one above, you have to send the request. The Ajax object has a send() method that is used to send the request. The syntax is,
myAjax.send(param);
myAjax is the Ajax object you created. The parameter is either null or a string. We shall see when you have to use null and when you have to use a string shortly.
Sending and Receiving Data
The main purpose of Ajax is to get small data from the server to the client very fast. To do this, Ajax calls an executable file at the server that will send the data; we have talked about this. Now, there is all kind of data in the server that can be sent to the client. Unless you want the executable file to be sending the same datum (singular of data) to the client all the time, while you make your request from the client, you can send information to the executable file that will enable it decide on a particular datum to send to the client. In other words, with Ajax you can send some small data to the executable file at the server, to enable it send a particular datum to you (browser).
So you can talk of the sending data and receiving data. Sending data is what the browser (Ajax) sends to the server. Receiving data is what the server sends back.
Example of Sending Data
The first and last name of a person is an example of data that can be sent to the server. The executable file at the server will use the person's full name to send data that concerns this particular person and not data in the server that concerns some other person.
With Ajax, you have to use either the GET method or the POST method; you do not use both in a request. Let us look at the details with the GET method, now. We shall look at the details of the POST method after.
The GET Method
Remember, when you want the GET method, you use "GET" as the first argument of the open() method of the Ajax object. Under this condition, we saw an example of the open() method as:
myAjax.open("GET", "http://www.somewebsite.com/sendtext.php", true);
The second parameter of the open() method is a URL, the third is always, true. With the GET method, the sending data is attached to the end of the URL, forming a long URL. Imagine that you wanted to send the first name and last name of somebody with the GET method. Also assume that the first name of the person is John and the last name is Smith. In this case, and with the GET method, the Ajax object open() method would be,
myAjax.open("GET", "http://www.somewebsite.com/sendtext.php?fname=John&lname=Smith", true);
Here, fname means first name and lname means last name. So in the URL, immediately after the executable file name, you type a question sign; after the question sign, you put the sending data. The sending data consists of name/value pairs. In this example there are two name/value pairs. For the first name/value pair, the name is fname and the value is John. For the second name/value pair, the name is lname and the value is Smith. The name/value pairs are separated with the & character.
If you do not have any sending data, there will be no question sign after the URL and no data after the URL.
So for the GET method the sending data is part of the URL. Do not confuse between the GET method and the Ajax send() method. The GET method is actually an HTTP method, which I said you do not have to worry about for the purpose of this article. The Ajax send() method is a method of the Ajax object. Remember that you do not have to type (create) the properties and methods of the Ajax object; they have already been done for you; you just use them.
The Ajax object carries out the three objectives that we mentioned above. So far we have talked about one of these objectives. The first objective is to open the connection: the Ajax object has the open() method for this. The second objective is to send the request: the Ajax object has the send() method for this.
Now, if you code for the GET method, then the Ajax send() method has to be,
myAjax.send(null);
That is, with the GET method, the single parameter is null.
So with our example and with the GET method, this is what you should have, so far, in your code:
var myAjax;
try
{
// Firefox, Opera 8.0+, Safari
myAjax = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
myAjax = new XDomainRequest();
}
catch (e)
{
try
{
myAjax = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
myAjax = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
}
}
}
}
myAjax.open("GET", "http://www.somewebsite.com/sendtext.php?fname=John&lname=Smith", true);
myAjax.send(null);
With the GET method, the Ajax object send() statement must immediately follow the Ajax object open() statement as shown above.
The POST method
Remember, when you want the POST method, you use "POST" as the first argument of the open() method of the Ajax object. Under this condition, our example of the open() method would be:
myAjax.open("POST", "http://www.somewebsite.com/sendtext.php", true);
The second parameter of the open() method is a URL, the third is always, true. Now, with the POST method, the sending data is NOT attached to the end of the URL, and so with the POST method, the URL is short. With the POST method, there can be no question sign after the URL. Note that the last argument for the Ajax object open() method is always true for both the GET and POST methods.
So, where then do you put the sending data with the POST method? With the POST method, the sending data is the parameter (string) of the Ajax object send() method.
Imagine that you wanted to send the first name and last name of somebody with the POST method. Also assume that this first name is John and the last name is Smith. This is the sending data we used for the GET method. In this case, and with the POST method, the above Ajax object open() method would be,
myAjax.send("fname=John&lname=Smith");
The sending data is a string argument of the Ajax send() method. Note that there is no question sign in front of the sending data argument of the POST method. You can replace the string parameter with a variable.
There is one other thing to accept with the POST method. With the POST method you have to include the following statement in between the Ajax object open statement and the Ajax object send statement:
myAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
Here, myAjax is the object you created. Just copy the rest of the statement as given above in your own code. So, for our example and for the POST method, you must have the following three statements together, in the order given:
myAjax.open("POST", "http://www.somewebsite.com/sendtext.php", true);
myAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
myAjax.send("fname=John&lname=Smith");
So, if you were coding for the POST method, for our example, this is what you should have typed so far:
var myAjax;
try
{
// Firefox, Opera 8.0+, Safari
myAjax = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
myAjax = new XDomainRequest();
}
catch (e)
{
try
{
myAjax = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
myAjax = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
}
}
}
}
myAjax.open("POST", "http://www.somewebsite.com/sendtext.php", true);
myAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
myAjax.send("fname=John&lname=Smith");
The setRequestHeader() method in the last but one statement above is actually an Ajax object method. Do not confuse the GET and POST methods with the Ajax object open(), send() and setRequestHeader() methods. The GET and POST methods are what are know as HTTP methods, which I said you should not worry about, so far as this article series is concern.
Note: if you do not have any data to send, then the argument of the Ajax send() method for POST should be null; in that case you would have,
myAjax.send(null);
Remember, with GET, this argument is always null.
Disadvantage of the GET method with Ajax
With the GET method, the amount of sending data is limited.
Advantage of the POST method
With the POST method, the amount of sending data is not limited.
When you are not sure of which method to use, use the POST method.
We have done enough for this part of the series. Let us take a break here and continue in the next part of the series, where we shall learn how to receive at the client, the response sent by the server.
Published by Chrys Forcha
I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a... View profile
- Beginning with Ajax and PHP: Book ReviewLee Babin's Beginning Ajax with PHP: From Novice to Professionaloffers a quick start in getting involved with Ajax and PHP.
- JSON FileIn this article, I explain the meaning of JSON File.
- Strings and the JavaScript Eval FunctionThis is the third part of my series, Mastering the JavaScript eval function. The string is considered.
- Microsoft Releases Internet Explorer 8On March 19, 2009, Microsoft Corp. released the first new version of its Windows Internet Explorer Web browser since 2006. In a press release Microsoft touts the new Internet Explorer 8 as offering "the best solution...
- Fix Windows and Internet Explorer Missing FilesUninstalling, reinstalling, and modifying Internet Explorer downloads result in a blank desktop screen. Affiliate branded Internet Explorer versions can confuse Windows.
- Making Ajax Request
- Making Ajax Response
- Making Ajax Response
- Ajax Chain
- Understanding Web Server Log FIles
- Hosting Your Own Web Server: Things to Consider
- What is AJAX?
