Home Web Front-end JS Tutorial Do you know the basic development of Ajax? Detailed explanation of the basic development steps of ajax (with examples)

Do you know the basic development of Ajax? Detailed explanation of the basic development steps of ajax (with examples)

Sep 10, 2018 pm 04:01 PM
ajax Development steps

This article mainly talks about the basic development steps of ajax. If you want to have a deeper understanding of ajax development, you must know the development steps. Although it is the most basic, it can also let people know how to use it. ajax development. Start reading this article now

  1. What is ajax? What is the use?
    The full name of Ajax is "Asynchrous Javascript And XML", which refers to a web development technology for creating interactive web applications. It is a technology for quickly creating dynamic web pages to improve the efficiency of interaction between browsers and servers. It can replace the traditional web interaction mode.

  2. How to understand this synchronization and asynchronousness?

    What is the traditional synchronous interaction model?
    In the traditional model, the synchronous interaction mode is used. The client browser makes a request to the server, and then waits until the server responds to the request, and cannot perform other operations.
    What is the asynchronous interaction model of mainstream technology?
    In the Ajax asynchronous interaction mode, there is an additional transfer object Ajax engine between the client browser and the server. When the client browser submits a request to the Ajax engine, the Ajax engine is responsible for transferring the request information to the server. , so that after the client browser sends the request, it can continue the operation instead of being in a waiting state. After the server processes the Ajax engine request, it passes the response to the Ajax engine, and the Ajax engine updates the client browser page.

  3. Ajax development steps
    Step one: Get the XMLHttpRequest object.
    You can find sample code in the xml document of w3school.

var xmlhttp = null;if(window.XMLHttpRequest){    //这个是针对浏览器IE7及其7以上的版。
    xmlhttp = new XMLHttpRequest();
}else if(window.ActiveXObject){    //这个是针对浏览器5到7的版本。
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
Copy after login
Copy after login

Step 2: Register the callback function.

 xmlhttp.onreadystatechange = function(){ 
};
Copy after login

onreadystatechange is an event handler for events triggered by each state change.

The third step: open("method","URL",async,"uname","pswd")
is only used to set the request method, URL path, and whether to synchronize. No request is sent.

ParametersFunction
methodSend request method, the optional parameters are: GET, POST and PUT.
URLThe URL path to be sent, you can use absolute paths and relative paths.
asyncWhether the request should be processed asynchronously. You can also leave it blank. The default value is true.
true, script processing will continue after send() is sent, that is, asynchronous processing.
false, the script will wait for a response before continuing script processing, that is, synchronous processing.
uname It has not been used yet, and the documentation does not explain it. Just don’t write it.
pswdIt has not been used yet, and the documentation does not explain it. Just don’t write it.


What if the URL requires parameters?
If it is a get request, concatenate the parameters directly after the url.
For example: xmlhttp.open("get","http://www.baidu.com?name=xiaosan");

If it is a post request, multiple setting operations must be performed:
For example: xmlhttp.open("post","http://www.baidu.com");
xmlhttp.setRequestHeader("content-type","application/x-www-form-urlencoded" );
xmlhttp.send("name=xiaosan");
The function of setRequestHeader("label","value") is to add the label/value pair to the http header that you want to be sent.

Step 4: send(content)
Send the request. If there are no parameters, it is written as send(null).

Step 5: Perform specific data operations in the callback function.
Before this, you need to understand some properties of XMLHttpRequest.

##onreadystatechangeState occurs When it changes, something can be triggered. readyStateSee the table below for detailsstatusNumber return status, such as "OK" is 200, "Not Found" is 404, etc.statusTextString return status, such as "OK", "Not Found", etc.responseTextReturn the response as a string. responseXMLReturns the response in XML. What is returned is an XML object that can be parsed using DOM
PropertiesDescription

The following table shows the status value of a readyState and the meaning of the status value:

Status value represents the status interpretation 0Uninitialized state, represents the loading state before the XMLHttpRequest object is created 1, open operation. 2Loaded status, send operation. 3In interactive status, response data has been received, but only the response header and the body have not been received. 4Complete status, all http response reception is completed.

  1. What is ajax? What is the use?
    The full name of Ajax is "Asynchrous Javascript And XML", which refers to a web development technology for creating interactive web applications. It is a technology for quickly creating dynamic web pages to improve the efficiency of interaction between browsers and servers. It can replace the traditional web interaction mode.

  2. How to understand this synchronization and asynchronousness?

    What is the traditional synchronous interaction model?
    In the traditional model, the synchronous interaction mode is used. The client browser makes a request to the server, and then waits until the server responds to the request, and cannot perform other operations. (If you want to see more, go to the PHP Chinese website AJAX Development Manual column to learn)
    What is the asynchronous interaction model of mainstream technology?
    In the Ajax asynchronous interaction mode, there is an additional transfer object Ajax engine between the client browser and the server. When the client browser submits a request to the Ajax engine, the Ajax engine is responsible for transferring the request information to the server. , so that after the client browser sends the request, it can continue the operation instead of being in a waiting state. After the server processes the Ajax engine request, it passes the response to the Ajax engine, and the Ajax engine updates the client browser page.

  3. Ajax development steps
    Step one: Get the XMLHttpRequest object.
    You can find sample code in the xml document of w3school.

var xmlhttp = null;if(window.XMLHttpRequest){    //这个是针对浏览器IE7及其7以上的版。
    xmlhttp = new XMLHttpRequest();
}else if(window.ActiveXObject){    //这个是针对浏览器5到7的版本。
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
Copy after login
Copy after login

Step 2: Register the callback function.
xmlhttp.onreadystatechange = function(){
};
onreadystatechange is an event handler for events triggered by each state change.

Step 3: open("method","URL",async,"uname","pswd")
Only used to set the request method, URL path, and whether to synchronize, and will not be sent. ask.

Parameters Function
method Send request method, the optional parameters are: GET, POST and PUT.
URL The URL path to be sent, you can use absolute paths and relative paths.
async Whether the request should be processed asynchronously. You can also leave it blank. The default value is true.
true, script processing will continue after send() is sent, that is, asynchronous processing.
false, the script will wait for a response before continuing script processing, that is, synchronous processing.
uname It has not been used yet, and the documentation does not explain it. Just don’t write it.
pswd It has not been used yet, and the documentation does not explain it. Just don’t write it.


What if the URL requires parameters?
If it is a get request, concatenate the parameters directly after the url.
For example: xmlhttp.open("get","http://www.baidu.com?name=xiaosan");

If it is a post request, multiple settings must be performed:
For example: xmlhttp.open("post","http://www.baidu.com");
xmlhttp.setRequestHeader("content-type","application/x-www-form-urlencoded" );
xmlhttp.send("name=xiaosan");
The function of setRequestHeader("label","value") is to add the label/value pair to the http header that you want to be sent.

Step 4: send(content)
Send the request. If there are no parameters, it is written as send(null).

Step 5: Perform specific data operations in the callback function.
Before this, you need to understand some properties of XMLHttpRequest.

##onreadystatechangeState occurs When it changes, something can be triggered. readyStateSee the table below for detailsstatusNumber return status, such as "OK" is 200, "Not Found" is 404, etc.statusTextString return status, such as "OK", "Not Found", etc.responseTextReturn the response as a string. responseXMLReturns the response in XML. What is returned is an XML object, which can be parsed using DOM
Properties Description

The following table shows the status value of a readyState and the meaning of the status value:

Status value represents the status interpretation 0Uninitialized state, represents the loading state before the XMLHttpRequest object is created 1, open operation. 2Loaded status, send operation. 3In interactive status, response data has been received, but only the response header and the body have not been received. 4Complete status, all http response reception is completed.
This article ends here (if you want to see more, go to the PHP Chinese website

AJAX User Manual column to learn). If you have any questions You can leave a message below to ask questions.

The above is the detailed content of Do you know the basic development of Ajax? Detailed explanation of the basic development steps of ajax (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the 403 error encountered by jQuery AJAX request How to solve the 403 error encountered by jQuery AJAX request Feb 20, 2024 am 10:07 AM

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

How to solve jQuery AJAX request 403 error How to solve jQuery AJAX request 403 error Feb 19, 2024 pm 05:55 PM

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

How to solve the problem of jQuery AJAX error 403? How to solve the problem of jQuery AJAX error 403? Feb 23, 2024 pm 04:27 PM

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

PHP and Ajax: Building an autocomplete suggestion engine PHP and Ajax: Building an autocomplete suggestion engine Jun 02, 2024 pm 08:39 PM

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

PHP vs. Ajax: Solutions for creating dynamically loaded content PHP vs. Ajax: Solutions for creating dynamically loaded content Jun 06, 2024 pm 01:12 PM

Ajax (Asynchronous JavaScript and XML) allows adding dynamic content without reloading the page. Using PHP and Ajax, you can dynamically load a product list: HTML creates a page with a container element, and the Ajax request adds the data to that element after loading it. JavaScript uses Ajax to send a request to the server through XMLHttpRequest to obtain product data in JSON format from the server. PHP uses MySQL to query product data from the database and encode it into JSON format. JavaScript parses the JSON data and displays it in the page container. Clicking the button triggers an Ajax request to load the product list.

What are the ajax versions? What are the ajax versions? Nov 22, 2023 pm 02:00 PM

Ajax is not a specific version, but a technology that uses a collection of technologies to asynchronously load and update web page content. Ajax does not have a specific version number, but there are some variations or extensions of ajax: 1. jQuery AJAX; 2. Axios; 3. Fetch API; 4. JSONP; 5. XMLHttpRequest Level 2; 6. WebSockets; 7. Server-Sent Events; 8, GraphQL, etc.

Asynchronous data exchange using Ajax functions Asynchronous data exchange using Ajax functions Jan 26, 2024 am 09:41 AM

How to use Ajax functions to achieve asynchronous data interaction With the development of the Internet and Web technology, data interaction between the front end and the back end has become very important. Traditional data interaction methods, such as page refresh and form submission, can no longer meet user needs. Ajax (Asynchronous JavaScript and XML) has become an important tool for asynchronous data interaction. Ajax enables the web to use JavaScript and the XMLHttpRequest object

See all articles