A simple understanding of JavaScript AJAX
This article mainly introduces the relevant knowledge of AJAX in JavaScript, which has a very good reference value. Let’s take a look at it with the editor
Abstract
AJAX technology is one of the necessary skills for web page construction. This article hopes to help everyone learn this technology easily
1. What is ajax?
ajax (asynchronous javascript xml) can refresh partial web page data instead of reloading the entire web page.
2. How to use ajax?
Step one: Create xmlhttprequest object
Create xmlhttprequest object, XMLHttpRequest object is used to exchange data with the server.
var xmlhttp =new XMLHttpRequest();
Step 2: Register the callback function
onreadystatechange function. When the server responds to the request and returns data, we want the client to process the data. We need to use the onreadystatechange function. The onreadystatechange function is triggered every time the readyState of the xmlhttprequest object changes. ReadyState will be introduced in detail in the next chapter.
xmlHttp.onreadystatechange= callback; function callback(){}
Step 3: Configure and send the request
Use the open() and send() methods of the xmlhttprequest object to configure and send the resource request to the server.
xmlhttp.open(method,url,async) method includes get and post, url is mainly the path of a file or resource, and the async parameter is true (representing asynchronous) or false (representing synchronization)
xmlhttp.send(); Use the get method to send a request to the server.
xmlhttp.send(string); Use the post method to send a request to the server.
Post form data needs to use the setRequestHeader method of the xmlhttprequest object to add an HTTP header.
When can post request be used?
(1)When updating a file or database.
(2) Send a large amount of data to the server because post request has no character limit.
(3) Send the encrypted data entered by the user.
xhttp.open("POST", "ajax_test.aspx", true); xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhttp.send("fname=Henry&lname=Ford");
Step 4: Process the response data
Use the responseText or responseXML attribute of the xmlhttprequest object to obtain the server's response.
Use the responseText attribute to get the string data of the server response, and use the responseXML attribute to get the XML data of the server response.
Use readyState==4 and status==200 in the callback function to determine whether the interaction is over and whether the response is correct, and obtain the data returned by the server as needed to update the page content.
function callback(){ if(xmlHttp.readyState == 4){ //判断交互是否成功 if(xmlHttp.status == 200){ //获取服务器返回的数据 //获取纯文本数据 var responseText =xmlHttp.responseText; document.getElementById("info").innerHTML = responseText; } } }
3. Five states (readyState) during AJAX operation
In the actual operation of AJAX, accessing XMLHttpRequest (XHR) is not completed all at once. It is the result obtained after going through multiple states respectively. There are 5 states in AJAX. These five states are switched and set by the AJAX engine, respectively.
0: XHR is defined but has not been initialized
1: The send() method is called and the request is being sent. After the request is sent, it starts waiting to receive the response
2 : Response reception completed
3: Response content is being parsed
4: Response content parsing completed, returned to client call
For the above status, the "0" status It is a status value that is automatically acquired after definition. For the status of successful access (information obtained), we mostly use "4" to judge.
It is worth noting that every time the state is switched, the onreadystatechange event will be triggered, so the onreadystatechange event is triggered 5 times in the entire process
4. Advantages of AJAX Disadvantages
Advantages
1. The biggest point is that the page does not refresh and communicates with the server within the page without interruption. The user's operation has a more rapid response capability, giving the user a very good experience.
2. Reduce the burden on the server. The principle of ajax is to "fetch data on demand", which can minimize the burden on the server caused by redundant requests and responses.
Disadvantages
1. Ajax kills the back button, which destroys the browser's back mechanism.
2. The support for search engines is relatively weak.
The above is the detailed content of A simple understanding of JavaScript AJAX. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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.

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 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

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:

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.

In order to improve Ajax security, there are several methods: CSRF protection: generate a token and send it to the client, add it to the server side in the request for verification. XSS protection: Use htmlspecialchars() to filter input to prevent malicious script injection. Content-Security-Policy header: Restrict the loading of malicious resources and specify the sources from which scripts and style sheets are allowed to be loaded. Validate server-side input: Validate input received from Ajax requests to prevent attackers from exploiting input vulnerabilities. Use secure Ajax libraries: Take advantage of automatic CSRF protection modules provided by libraries such as jQuery.

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
