Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Ajax Communication principle XMLHttpRequest

Shulou Source: shulou.com Published: 2022-06-03 06:01:25 11月17日 Update

This article mainly shows you the "sample analysis of Ajax communication principles XMLHttpRequest", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn the "Ajax communication principles XMLHttpRequest example analysis" this article.

Obviously, AJax is a technology that uses JavaScript scripts to access data.

AJAX enables web pages to be updated asynchronously. This is to make a partial update to the page without reloading the entire page.

XMLHttpRequest is the key to AJAX

Browsers now support XMLHttpRequest objects (IE5 and IE6 use ActiveXObject).

There are five states of readyState for requesting data from the background: server is not initialized, 1: server connection has been established, 2 requests have been accepted, 3 requests are being processed, 4 requests are completed.

Every time you change the state, you can trigger an onreadystatechange event. Status has two states: 200,404: page not found.

Let's take a look at an Ajax foreground implementation code:

The copy code is as follows:

Untitled page

Function getName () {

Var xmlhttp

If (window.XMLHttpRequest) {

Xmlhttp=new XMLHttpRequest ()

} else {

Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP")

}

Xmlhttp.onreadystatechange=function () {

If (xmlhttp.readyState==4 & & xmlhttp.status==200) {

Alert ("Hello:" + xmlhttp.responseText)

}

}

Xmlhttp.open ("post", "Default.aspx?id=gname", true)

Xmlhttp.send ()

}

Tags: Code principle example communication analysis content status article web page background that is data server learning help update service two events. Hello. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Linux Apple vpn Shulou Technology