AJAX
The full name Asynchronous JavaScript and XML
of , which means asynchronous JavaScript
sum XML
, is a way to interact with the server and update the content of the page locally without reloading the entire page, not a new language.
AJAX
Its use is independent of platforms and browsers, and does not require any plug-ins, just browser support JavaScript
.
working principle
AJAX
The working principle is very simple, XmlHttpRequest
send an asynchronous request to the server through the object, then obtain the data from the server, and then use JavaScript
it to operate DOM
the element, and then realize the function of locally updating the page content without refreshing the page.
Among them, the core is the XmlHttpRequest
object, all operations are carried out around this object, and the main contributor to the realization of asynchronous request is XmlHttpRequest
the function in the object onreadystatechange
.
object creation
All current browsers support XMLHttpRequest
objects, while IE 5
and IE 6
uses ActiveXObject
objects.
Because we don't know what type of browser the user is using, we need to check if the browser supports XMLHttpRequest
objects.
var ajaxobj;
// 判断浏览器是否支持 XMLHttpRequest 对象
if (window.XMLHttpRequest) {
ajaxobj = new XMLHttpRequest();
} else {
// 由于IE 5、IE 6不支持 XMLHttpRequest 对象
// 所以创建 ActiveXObject 对象
ajaxobj = new ActiveXObject("Microsoft.XMLHTTP");
}
send request
In this step, we need to use XMLHttpRequest
two methods of the object:
open(method, url, async)
method;send(postData)
method.
open(method, url, async)
The function of the method is to set whether the request is GET
or not POST
(of course PUT
, DELETE
), set the requested server resource URL
, and set whether it is an asynchronous request.
send(postData)
The function of the method is to send the request. In the GET
mode, you do not need to pass in parameters, and in the POST
mode, you need to fill in the incoming parameters postData
.
for example:
ajaxobj.open("GET", "https://www.vingyun.com/ajaxTest?nickname=ving", true);
ajaxobj.send();
request status
XMLHttpRequest
Objects contain two properties for storing state:
readyState
Attributes;status
Attributes.
readyState
Attributes are used to represent XMLHttpRequest
the state of an object. The values and descriptions are as follows:
value | describe |
---|---|
0 | request not initialized |
1 | Server connection established |
2 | request received |
3 | request processing |
4 | The request is complete and the response is ready |
status
Attributes are used to represent the HTTP
state of the connection, the value and meaning refer to the HTTP
protocol, we only need to know status=200
that the request is successfully completed and the requested resource is sent to the client.
In addition, the XMLHttpRequest
object contains a listener function onreadystatechange
, which is executed whenever readyState
the property changes, which is the key to the realization of asynchronous requests.
Therefore, we can easily perform the next task based on the request status with the following code :
xmlhttp.onreadystatechange = function()
{
if (ajaxobj.readyState == 4 && ajaxobj.status==200)
{
// 成功后的操作代码
}
}
response content
XMLHttpRequest
The object also contains the content of the server response, XML
stored in two properties as a string and in two forms:
responseText
Attributes;responseXML
Attributes.
responseText
The response data in the form of a string is stored in the attribute. When parsing the data, it can be processed in the same way as the string.
responseXML
The response data in the form is stored in the attribute. XML
When parsing the data, you need to follow XML
the parsing method of the document.
Complete request example
var ajaxobj;
// 判断浏览器是否支持 XMLHttpRequest 对象
if (window.XMLHttpRequest) {
ajaxobj = new XMLHttpRequest();
} else {
// 由于IE 5、IE 6不支持 XMLHttpRequest 对象
// 所以创建 ActiveXObject 对象
ajaxobj = new ActiveXObject("Microsoft.XMLHTTP");
}
// 发送请求
ajaxobj.open("GET", "https://www.vingyun.com/ajaxTest?nickname=ving", true);
ajaxobj.send();
// 根据请求状态来执行下一步任务
xmlhttp.onreadystatechange = function()
{
if (ajaxobj.readyState == 4 && ajaxobj.status==200)
{
// 成功后的操作代码
}
}
Use callback function
One inconvenience of the above example is that the operation code after a successful request is hard-coded in the request and cannot be reused.
So we introduce the concept of callback function , give a popular example:
I'm going to visit a friend ( I'm going to execute a function ), but the friend is not at home ( but the time has not come ), I don't know when he will come back ( and I don't know when the time will come ), as usual Practice, I can come back next time ( just keep looping to see if the time is ripe ), but I may not be there next time ( keep failing ), if I visit again and again and my friends are not at home, it will take too much time There are too many ( this is too expensive ), so I asked my friend's servant to tell me when my friend came home (the time is up ), and I can come to visit ( function execution ).
So we can change the above example to the following:
var ajaxobj;
// 判断浏览器是否支持 XMLHttpRequest 对象
if (window.XMLHttpRequest) {
ajaxobj = new XMLHttpRequest();
} else {
// 由于IE 5、IE 6不支持 XMLHttpRequest 对象
// 所以创建 ActiveXObject 对象
ajaxobj = new ActiveXObject("Microsoft.XMLHTTP");
}
// 发送请求
ajaxobj.open("GET", "https://www.vingyun.com/ajaxTest?nickname=ving", true);
ajaxobj.send();
// 根据请求状态来执行下一步任务
// 这是一个函数的引用,而不是函数的调用,因此务必确保不要加括号
xmlhttp.onreadystatechange = ajaxSuccess;
// 我是一个独立的函数,独立的!!!
function ajaxSuccess()
{
if (ajaxobj.readyState == 4 && ajaxobj.status==200)
{
// 成功后的操作代码
}
}