Computers, Programming
Ajax - what is this? Asynchronous JavaScript and XML: advantages and disadvantages
Now you can observe that the use and application of new technologies is gaining popularity on the Internet. One of them is AJAX. What it is? What characteristics does it have? Where is it applied? All of these questions can be answered within the framework of this article.
general information
And what is special about it?
So, we already have a general idea of what AJAX is. What does this give us in practice? Why are there active conversations about its application? The peculiarity of this technology is that when you use it, you do not need to update the whole page at the first need. After all, you can request new data only for part of it. This is convenient (because you have to wait less time), and economically (for those who do not have unlimited Internet). However, to inform the user about the update, it is desirable to use at least a download indicator or text messages that will inform about new data from the server. As a minus of technology is often mentioned the fact that it is not supported by obsolete and text-based browsers. Yes, and the ability to disable Javascript technology is the user, than many and use. Therefore, do not abuse AJAX. What can it do if you do not provide for alternative methods of presenting information? At best, the information will simply not be displayed on the site. At worst, its appearance will suffer so much that the user will not return to it any more.
Advantages of "Ajax"
- You can create a convenient web application, in which the interface will have a number of user-friendly features that will eventually facilitate the interaction of the person and the site.
- Instead of a full page reload, a partial reboot occurs. Thanks to this, the resources of the machine are saved and the speed is optimized.
- The server part of the site actively interacts with the user.
- It is convenient to use to implement a number of ideas.
Data exchange
Here, for example, we need a feedback form, through which it will be possible to advise clients. How can it be done? First, you need to take care of the "bearer" of information. To do this, create an XMLHttpRequest object. It acts as an intermediary between the server and the user's browser. With this object, queries will be sent, as well as answers to them. For direct data exchange we need AJAX PHP. This toolkit can be implemented using GET or POST requests. In this case, the necessary arguments will be passed directly through the URL. At the same time, a function will be launched to ensure that data transmission is not interrupted. But which AJAX request is implemented? Recommend using POST. Thanks to him, you can transfer an unlimited amount of data, which, moreover, is much more difficult to access.
Principle of operation
Server Response
It can come not only through XML, as one might think because of the name of the technology. In addition, it can be passed as plain text or JSON. In the first case, the information received can be immediately displayed on the page. If XML is used, care must be taken to ensure that the AJAX file is properly processed in the client's browser. All data in this case is converted to (X) HTML. When the JSON format is used , you must execute the code to get a full Javascript object. It is necessary to be cautious and take into account the fact that many intruders like to transfer malicious code through this technology. Therefore, it is necessary to provide a procedure for checking the data received before processing them. The implementation features for different browsers may vary a little, but, in general, the scheme of the action is one.
We make a request to the server and process the response
- Initially, you need to create and verify that there is XMLHttpRequest.
- Then, the connection to the server is initialized.
- We send a request to it.
- We wait until the data comes, and we process them.
It is necessary to tell about some features concerning creation of object. It can be initiated anywhere, wherever possible in the programming language. But if you assign it global status, then at any one time it will be possible for him to work with only one query. This moment is necessary to pay close attention. Concerning processing - here we are guided by the chosen tool. Thus, with XML, data must be processed by DOM functions and presented to the user already the final result in HTML. And what about JSON? It should be understood that it is an object notation for Javascript. What does it mean? Thanks to JSON, you can represent an object as a string. But it must be remembered that this method is not completely safe, and a lot of factors will need to be taken into account. But, in general, it is very easy.
Server Programming Languages
Virtually any web application uses them in one way or another. For data transfer, the most popular is PHP. It usually writes files for sites where the speed of information transfer is not critical, but gives priority to the balance between it and reliability. Methods for passing XMLHttpRequest were discussed above.
Configuring the server
- HTML / XHTML. They are needed for the server to recognize the layout of the page.
- CSS. This tool is necessary for visual selection of page design.
- DOM. It is needed to provide dynamic changes to the page as a response to the action on the part of the user.
- XML. Required to transfer data between the client and the server.
- JavaScript. It is necessary to create the engine "Ajax" and provide interactivity.
- XMLHttpRequest. The object by which requests to the server will be forwarded.
Use on the site
So, now we have both theoretical knowledge and ideas about how AJAX works. Sending data now requires only a certain practice. Let's see in general how one can approach the implementation of the task with the use of "Ajax". So, we need to create a database of drivers. And they will differ in the devices on which they can work. It should be noted that the database in question is very large, so simply forwarding it to the client and already offering him to make a selection using JavaScript does not make sense. It is also not desirable that the web page itself be rebooted due to one parameter. On the server, requests will process special PHP scripts. The database is implemented as an XML file.
About the database
We are working on the work of the engine
How will everything happen? So, there comes a certain event, which will cause a special function. It will prepare GET or POST data that will be forwarded. Then, a function is called that will call the URL of the server script. It must provide for the creation of an XMLHttpRequest object. A reference to it should be stored in a variable. After the data has been sent to the server, it is necessary to wait for an answer from it. To do this, you can turn on the "wiretap" until information is received (if you know what will come and when) or provide a function that will always be ready to receive data. The first option is less reliable in the event of a freelance situation. The second implementation is more expensive in terms of resources. Although, if this is done only with respect to one variable, it will not affect the fast Internet. But if there are hundreds and thousands of them, and they will be large, then it will be another matter. Therefore, it is necessary to seek a balance between the use of available resources and efficiency. After all, it should be understood that not everyone has 16 GB of RAM. To do this, you can set a certain waiting time, after which the previous data is substituted or information that the server is not available at this time.
Features
Conclusion
So this article comes to a logical conclusion. If we talk about "Ajax" in general, then this tool allows you to create very attractive and multifunctional sites that work fast and with competent layout deliver their users a pleasure. We can not doubt that in the future there will be even more advanced technologies, but for now we will use what we have. As the programming languages and their methods are improved, it is also possible that such an option will become available that will allow the creation of an even better product. But anyway, it is necessary to work now with what is. And the name is "Ajax".
Similar articles
Trending Now