1. What is Ajax?
The Ajax full name is asynchronous JavaScript and XML, an abbreviation for asynchronous JavaScript and XML. Ajax technology is used to create Web sites for interactive Web applications, and for what is asynchronous, the text will be interpreted later.
1.1 Desktop applications and Web applications
Before discussing Ajax techniques in detail, you need to know exactly what Ajax technology is for. Currently, there are two basic types of authoring applications:
Desktop Applications (Desktop application)
Web application (Web application)
Desktop applications can be obtained from the Internet or by CD, and need to be run on desktop computers, such as some of our common PC software. Unlike Web applications, Web applications run on a Web server somewhere, so they need to be accessed through a Web browser.
However, more important than where the code of these applications are running is how the application works and how it interacts with it. Desktop applications are generally faster and have a nice user interface and extraordinary dynamics, you can click, select, open menus and submenus, cruise around, and do not have to wait; on the other hand, Web applications, such as Amazon.com and ebay, provide services that desktop programs cannot achieve. However, as the web is strong, waiting waits, waits for the server to respond, waits for the screen to refresh, waits for the request to return and produces the new interface.
The advent of Ajax is to mitigate the problem of waiting in Web applications compared to desktop applications.
1.2 AJAX-old technology, new faces
Ajax technology is not really a new technology, but the integration of several other existing technologies.
Ajax applications use the following basic techniques:
Use HTML and CSS to create Web Forms and represent web page information;
Use JavaScript to manipulate Dom (Document Object Model) for dynamic display and interaction;
Asynchronous data exchange with the Web server using the XMLHttpRequest object;
Use XML for data exchange and related operations;
Use JavaScript to bind everything together.
We are here to further analyze the responsibilities of these technologies. Now I just need to familiarize myself with these components and technologies. The more familiar these codes are, the easier it is to move from a fragmented understanding of these technologies to a real grasp of those technologies (and also really open the door to WEB application development).
XMLHttpRequest objects
One of the objects you want to know is probably the most unfamiliar to you, XMLHttpRequest. This is a JavaScript object, and creating the object is simple, as shown in Listing 1.
Listing 1. Create a new XMLHttpRequest object
The code is as follows:
<script language= "javascript" type= "Text/javascript" >
<!--
var xmlHttp = new XMLHttpRequest ();
-->
</script>
You'll learn more about this object later, and now you know that this is the object that handles all server traffic. Before you go on, stop and think: The JavaScript technology is a conversation with the server through the XMLHttpRequest object. This is not a generic application flow, which is precisely the source of the powerful functionality of Ajax.
In a typical WEB application, users fill Out form fields and click the Submit button. The entire form is then sent to the server, and the server forwards it to the script that handles the form (usually PHP or Java, or maybe a CGI process or something like that), and then sends it back to the new page after the execution of the script completes. The page may be HTML with a new form that has already populated some data, a confirmation page, or a page that has some options selected based on the input data from the original form. Of course, users must wait when a script or program on the server processes and returns a new form. The screen becomes blank until the server returns data and then redraw it. This is why interactivity is poor, and users don't get immediate feedback, so they feel different from desktop applications.
Ajax basically puts JavaScript technology and XMLHttpRequest objects between Web forms and servers. When a user fills out a form, the data is sent to some JavaScript code instead of being sent directly to the server. Instead, the JavaScript code captures the form data and sends the request to the server. Also, the form on the user's screen does not blink, disappear, or delay. In other words, JavaScript code sends a request behind the scenes, and the user doesn't even know the request is sent. Better yet, requests are sent asynchronously, meaning that JavaScript code (and users) does not have to wait for a response from the server. So users can continue to enter data, scroll the screen, and use the application.