Asynchronous JavaScript and XML (AJAX)
When to become it is not enough existing opportunities, and to improve existing already there is no place, then there is a technological break. Such break also is AJAX (Asynchronous JavaScript and XML) - the approach to construction of the user interfaces of webs - applications at which the web-page, not being reloaded, itself loads in addition the data necessary to the user. AJAX - one of components of concept DHTML.
That this technology gives us. Now development WEB of applications aspires to differentiation of a client part and server, it and causes universal use of patterns, such as Smarty and XSLT. Now projects become more complex , and to bind among themselves various technologies to become expensively for time of the developer. So, for example, all styles of formatting are born{taken out} in CSS or in XSL files, HTML or XML the data are stored{kept} in other sections, server obrabotchiki in the third, databases in the fourth. And if 5-6 more years back practically the interlacing of all it in one file now all this more often to become a rarity everywhere was possible to see.
By development of more complex projects there is a necessity for structure and udobochitaemosti a code. It is not necessary to litter a code of the programmer with a code verstal`hhika, and a code verstal`hhika - pravkami the designer, and so on.
There is a necessity for differentiation of job. So, for example, the designer will do{make} the job, verstal`hhik the, the programmer the, and thus anybody will not prevent to each other. In a result to each participant of the project will know only those data with which he should work enough. In that case productivity of group and quality of the project raises in times. Now this problem with success is solved by use of patterns, however it too creates the certain difficulties as, say, to connect Smarty, it is necessary to connect the program module of processing of patterns, and precisely to connect with structure of the project. And it not always probably also demands the certain expenses. Little bit easier at use of sheaf XML + XSL as they give more opportunities, however it is alternative, no more. And that if to see aside something considerably new, that would allow to unit all better, using opportunities of what is? Try to present JavaScript which possesses all opportunities PHP or Perl, including job with grafikoj and databases which has much more convenient expansibility and a practicality, and besides cross-country - platformen.
So such AJAX? For the first time about Ajax have started talking after occurrence in February of 2005 year of clause{article} of Dzhessi James Garretta (Jesse James Garrett) " the New approach to webs - applications ". Ajax is not independent technology. It is idea which is based on two main principles.
Use DHTML for dynamical change of the maintenance{contents} of page.
Use XMLHttpRequest for the reference{manipulation} to the server "hurriedly".
Use of these two approaches allows to create much more convenient WEB-interfaces of the user on those pages of sites where active interaction with the user is necessary. Use Ajax became most popular after company Google began to use actively it at creation of the sites, such as Gmail, Google maps and Google suggest. Creation of these sites has confirmed efficiency of use of the given approach.
So is more detailed: if to take classical model of the WEB-application:
The client, typing{collecting} in a line of search the address of his interesting resource, getting on the server, does{makes} to it search. The server makes calculations according to search, addresses to a database and so on then the received data go to the client and, are in case of need substituted in patterns and processed by a browser. Result is the page which we see and which 80 % of the population of the country taking place in WEB name the Internet. It is classical model which has had time to recommend and deserve itself to itself a place of honour under the sun. It is the most simple model of interaction and, as the consequence{investigation}, the most widespread. However her even more often to become insufficiently. Imagine, on-line game " Sea fight " in which play two inveterate friends - the inhabitant of the republic of South Africa and the inhabitant of Japan. How with the help of such model to make their game maximum pleasant? In any case the data of the sunk ships will be is stored{kept} on the server, whether and what to check up the opponent resembled, will be necessary to update each time page and podguhhat` the out-of-date data. " But in fact people have thought up caching " - you will say and will be absolutely right, but it is easier to not become from it. Caching only will speed up time of interaction with the server, but will not relieve of necessity to reload page. As certain time of self-updating, but also in this case page is possible to put a variant will be reloaded completely.
Now we shall see at model of interaction AJAX:
The sequence of actions of the client is saved also he, most likely will not understand that will occur, and word AJAX will associate only with the name of football club. But on the party of the server all looks differently.
At the reference{manipulation} to the server, the page which will be displayed to the user is generated, and to suggest him to make his interesting sequence of actions. At conscious (though and it it is not necessary) a choice of the client, his search will address to AJAX to the module which will make all his interesting calculations and job with the server as such. But in what an innovation? The basic difference in that that this method enables us to address dynamically to the server and to carry out actions interesting us. For example, we need to execute the reference{manipulation} to a database and to obtain the data interesting us which then we shall use. We shall store{keep} the data in XML a file which will be dynamically formed, thus:
We create new object JavaScript:
var req = new ActiveXObject (" Microsoft. XMLHTTP "); (for IE)
var req = new XMLHttpRequest (); (For all rest)
Then we write function using this object
var req;
function loadXMLDoc (url) {
// branch for native XMLHttpRequest object
if (window. XMLHttpRequest) {
req = new XMLHttpRequest ();
req.onreadystatechange = processReqChange;
req.open ("GET", url, true);
req.send (null);
// branch for IE/Windows ActiveX version
} else if (window. ActiveXObject) {
req = new ActiveXObject (" Microsoft. XMLHTTP ");
if (req) {
req.onreadystatechange = processReqChange;
req.open ("GET", url, true);
req.send ();
}
}
}
Body HTML of a file we write a script which will be:
function checkName (input, response)
{
if (response! = ") {
// Response mode
message = document.getElementById (' nameCheckFailed ');
if (response == ' 1 ') {
message.className = ' error ';
} else {
message.className = ' hidden ';
}
} else {
// Input mode
url = ' http: // localhost/xml/checkUserName.php? q = ' \
+ input;
loadXMLDoc (url);
}
}
In a file localhost/xml/checkUserName.php we process the data received from the command line in this case in a variable q. And result we save in XML to structure which we store{keep} in the same file. So we can receive and process the data received from a DB, or something another necessary for us. Besides the server will process only those data which to us are necessary for updating, instead of all page in case of her perezagruzki.
Now we shall return to two friends - to fans{amateurs} of sea fight: in view of occurrence of the given innovation, we can make the following: to put check during each three seconds XML of a file the given check means itself check of a database for new recording, that is - the course made by the opponent. If the course has been made, the page without perezagruzki heats the ships, thus, spoiling mood to participants of water fights. The given functionality is reached{achieved} by elementary use Javascript and tables of styles. This example evident enough, however not full, application of the given technology is much more essential.
However not all so is simple. Let's consider negative features now.
First - we can pass given only method GET, accordingly great volumes of the data should be let alone. The given problem more than once rose in different sources, but sirs, are in fact Sookies which are quite comprehensible in case of transfer of the big data, than can contain in itself GET search, and Javascript in turn has functions for job with them.
The second problem - cross-country - brauzernost`. Object XMLHttpRequest yet is not a part of any standard (though something similar was already offered in specification W3C DOM Level 3 Load and Save). Therefore there are two excellent{different} from each other a method of a call of this object in a code of a script. In Internet Explorer object ActiveX is caused so:
var req = new ActiveXObject (" Microsoft. XMLHTTP ");
In Mozilla and Safari it is done{made} easier (as there it is the object which has been built - in in JavaScript):
var req = new XMLHttpRequest ();
All modern browsers support the given object and problems will arise only at 1,8 % of users (it agrees the data of statistics of company SpyLog) which use very old versions of browsers which are not supporting this object.
And, at last, security. On it we shall stop more in detail. The basic problem consists that all data and initial code JavaScript of functions can be seen by viewing an initial code of page, accordingly the malefactor can track logic of performance of searches and at the certain coincidence of circumstances to execute a set of commands necessary for him. It does not play special a role, when we have simple comparison of the data but what to do{make} in more complex situations, for example at authorization and how in that case to pass passwords. As already it has been said above, to the aid come Cookies. The necessary data can be sent with their help as them and to process. We shall take an example in which the user will pass autentifikaciju with which help of technology clause{article} is devoted.
Generation of page, we form unique values which then we place in variables of the server. And in Cookies a browser, then at authorization we receive a login name and his password which to us are necessary for passing the module of processing on the server.
After the user has entered the data and has pressed button Submit his password to be brought in Cookies, and the login name is passed openly - by the link for example http: // www.mubestajax.com/ajax.php? login=pupkin at data acquisition the server, first of all spends verification of the received data. As which we generated values from the beginning of job of the server and then passed their global variables of the server and cookies should coincide, at check of integrity of the transferred{handed} data in case of discrepancy the program ceases to work. If all has passed well all necessary data are taken and necessary calculations and jobs are spent. Such way of protection is simple enough and effective. But for the big projects he will not approach.
When on the foreground safety leaves, it is better to use more complex and reliable decisions. But in most cases the given safety measures will be more than are part of the standard software of modern servers, which basic feature - simplicity enough as use of more complex modules entails use of technologies which not always. On this such technologies as MySQL and PHP have received very much a wide circulation since they provide simplicity of job at small resursoemkosti and sufficient reliability. And to frameworks of the given software the decision offered{suggested} as well as possible will approach above.
In the conclusion it would be desirable to say, that AJAX on the one hand - huge jump in WEB engineering, but on the other hand it for a long time nazrevavshij a stage of evolution which has opened new horizons in sphere of development of the software. During too time this technology of still rather "cheese" as its{her} use at present more likely pleasant exception. But I am confident, that about her yet everyone will hear time.

|