What Is Ajax?
Ajax is "Asynchronous JavaScript + XML" (Asynchronous JavaScript and XML), which is a Web page development technology used to create interactive web applications. It can quickly obtain the required data and content from the server, and achieve local refreshing so that users can better browse the website. When there is no Ajax, the webpage submission form must be waited and refreshed. At this time, the user must wait for the response from the server, and the user cannot do anything else on the current page. With Ajax, users do not need to wait when submitting a form. They can browse other things on the page. After the form is submitted, the server can quickly return the required data and webpages, the webpage does not need to be refreshed.
What can Ajax do?
When there is no Ajax, when we perform form verification, we must enter it before submitting the page to check whether the information is legal and valid, and the page must be refreshed, in this way, the user experience is very poor. The information entered by the user through hard work is found to be incorrect and illegal only when submitted, which leads to a poor user experience. After Ajax technology is used, checking the form information can quickly obtain information from the server without refreshing it, making it very convenient for users to use it. AJAX can also achieve partial refresh, just like Google Maps.
Next, let's learn how to use Ajax?
Before using Ajax, you must have a good grasp of the XMLHTTPRequest object, because Ajax is used around the XMLHTTPRequest object. Ajax usage:
(1) first create the XMLHTTPRequest object
var xmlhttp;if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); }else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
(2) Call the XMLHttpRequest method open (), and send ()
XMLHTTP. Open ("get", "URL path", true); XMLHTTP. Send ();
(3) The server returns responsetext
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
The value returned by the server is assigned to the tag of mydiv.
However, the HTTP status must be determined, and the code is as follows:
xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } }