Home>Article>Web Front-end> In-depth understanding of ajax (detailed picture and text explanation)
1.1 What is ajax:
- Ajax is "Asynchronous Javascript And XML" (Asynchronous JavaScript and XML), Refers to a web development technology for creating interactive web applications. Ajax = Asynchronous JavaScript and XML (a subset of Standard Universal Markup Language). Ajax can use web pages to achieve asynchronous updates by exchanging a small amount of data with the server in the background. This means that parts of a web page can be updated without reloading the entire web page (no refresh technology). Traditional web pages (not using Ajax) must reload the entire web page if the content needs to be updated.
1.2Ajax application scenarios:
1.2.1 Check whether the user name has been registered:
Many sites The registration page has a friendly prompt to automatically detect whether the user name exists. The entire page of this function is not refreshed, but data can still be exchanged asynchronously with the server to query whether the user name entered by the user exists in the database.
1.2.2 Province and city level drop-down box linkage:
Many sites have the operation of inputting the user's address. When completing the address input, the user's location The province is a drop-down box. When selecting different provinces, different city selections will appear. This is the most common province-city linkage effect.
1.2.3 Content auto-completion:
Both Baidu, which focuses on search, and Taobao, which searches for products within the site, have search functions. , when you enter a query keyword in the i search box, the entire page is not refreshed, but relevant query notes will be displayed based on the keywords. This process is asynchronous.
Baidu’s search completion function:
Taobao’s search completion function:
1.3 The difference between synchronous mode and asynchronous mode:
- Send a request in synchronous mode: to send a request, you need to wait for the response to return before you can send the next request. If There is no response to this request and the next request cannot be sent. The client will always be waiting.
- Send a request asynchronously: Send a request without waiting for the response to be returned. You can send the next request at any time, that is, there is no need to wait.
1.4 Principle analysis of Ajax:
- The AJAX engine will send an asynchronous request without refreshing the browser address bar:
- Use JavaScript to obtain the browser's built-in AJAX engine (XMLHttpRequest object)
- Use js to determine the request path and request parameters
- The AJAX engine object sends the request according to the request path and request parameters
- The server receives the request from the Ajax engine and processes it :
- The server obtains the request parameter data
- The server processes the request business (calling the business layer code)
- The server responds with data to the Ajax engine
- The Ajax engine obtains the data responded by the server and updates the data to the specific location of the browser page by executing the JavaScript callback function:
- By setting it to the Ajax engine The callback function obtains the server response data
- Use JavaScript to display the response data at the specified location, thereby partially modifying the page data and achieving partial refresh.
2.1js native Ajax:
- js native Ajax development steps:
Create Ajax engine object
Bind listening for the Ajax engine object (the listening server has responded with data to the engine)
Bind submission address
Send request
Listen and process the response data
rrreeTitle
2.2 Ajax engine connection status readyState value 0~4 change process:
- 存有 XMLHttpRequest 的状态。从 0 到 4 发生变化。
- 0: 请求未初始化
- 1: 服务器连接已建立
- 2: 请求已接收
- 3: 请求处理中
- 4: 请求已完成,且响应已就绪
这里状态值4只能说明接收到了服务器的响应服务器处理ajax请求结束,但是不能代表正确的获取了服务器的响应,需要配合http状态码200两个条件就可以说明正确的获取了服务器响应。只有这两个条件满足,xmlhttp.responseText才可以获取到正确的响应数据。
xmlhttp.onreadystatechange = function(){ if(xmlhttp.readyState == 4){ if(xmlhttp.status == 200){ alert("响应数据" + xmlhttp.responseText); } } };
感谢大家的阅读,希望大家收益多多。
本文转自:https://blog.csdn.net/Huangyuhua068/article/details/82889614
推荐教程:《JS教程》
The above is the detailed content of In-depth understanding of ajax (detailed picture and text explanation). For more information, please follow other related articles on the PHP Chinese website!