


Difference analysis between ajax synchronous requests and asynchronous requests_javascript skills
The difference between ajax synchronization and asynchronous, let’s look at 2 pieces of code first:
Code 1:
Synchronize = function(url,param) {
function createXhrObject() {
var http;
var activeX = [ "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP" , "Microsoft.XMLHTTP" ];
try {
http = new XMLHttpRequest;
} catch (e) {
for (var i = 0; i < activeX.length; i) {
try {
http = new ActiveXObject(activeX[i]);
break;
} catch (e) {}
}
} finally {
return http;
}
}
var conn = createXhrObject();
conn.open("POST", url, false);//ajax synchronization
conn.send(param);
var strReturn = conn.responseText;
alert("1");
if (strReturn != "") {
return Ext.decode(conn.responseText);
} else {
return null;
}
alert("2");
};
Code 2:
Ajax synchronous request method:
Synchronize = function(url,param) {
function createXhrObject() {
var http;
var activeX = [ "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP" ];
try {
http = new XMLHttpRequest;
} catch (e) {
for (var i = 0; i < activeX.length; i) {
try {
http = new ActiveXObject(activeX[i]);
break;
} catch (e) {}
}
} finally {
return http;
}
}
var conn = createXhrObject();
conn.open( "POST", url, true);//ajax asynchronous
conn.send(param);
var strReturn = conn.responseText;
alert("1");
if (strReturn ! = "") {
return Ext.decode(conn.responseText);
} else {
return null;
}
alert("2");
};
The difference between synchronous and asynchronous is as follows:
conn.open('POST',Url,true); // ajax asynchronous
conn.open('POST',Url ,false); // ajax synchronization
For code two, it is an asynchronous ajax request. The execution result is: first execute alert(2) and then execute alert(1). Asynchronous means that once conn.open As soon as the request is sent, the front end executes the following code without waiting for its response, so alert(2) is executed first, and then alert(1) is executed when the response arrives;
For code one, it is The execution result of a synchronous ajax request is: execute alert(1) first and then alert(2). Synchronization means that once the conn.open request is issued, the front end will wait for its response. After the response is completed, alert(1 ) is executed first, then alert(2);

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

As web applications continue to develop and change, handling parallel and asynchronous requests has become an important topic in PHP backend API development. In traditional PHP applications, requests are performed synchronously, that is, a request will wait until a response is received, which will affect the response speed and performance of the application. However, PHP now has the ability to handle parallel and asynchronous requests. These features allow us to better handle a large number of concurrent requests and improve the response speed and performance of the application. This article will discuss how to deal with PHP backend API development

How to solve the problem of real-time update of asynchronous request data in Vue development. With the development of front-end technology, more and more web applications use asynchronous request data to improve user experience and page performance. In Vue development, how to solve the problem of real-time update of asynchronous request data is a key challenge. Real-time update means that when the asynchronously requested data changes, the page can be automatically updated to display the latest data. In Vue, there are multiple solutions to achieve real-time updates of asynchronous data. 1. Responsive machine using Vue

UniApp error: 'xxx' asynchronous request failed solution With the rapid development of mobile applications, UniApp, as a cross-platform development framework, is increasingly favored by developers. However, like any other technical framework, UniApp also has some potential problems, one of which is the problem of error reporting when asynchronous requests fail. This article will introduce some common reasons why UniApp reports error: "'xxx' asynchronous request failed" and provide some solutions. First, we need to understand what an asynchronous request is. in U

In today's Internet era, many applications need to make network requests to obtain or send data. HTTP requests are one of the most commonly used network request methods. In the Go language, we can use the net/http package in the standard library to initiate HTTP requests, but this will block the current coroutine. So how to implement asynchronous HTTP requests in Go language? This article will introduce two methods of implementing asynchronous HTTP requests in the Go language. Method 1: Use goroutine and channelGorout

Asynchronous request processing problems encountered in Vue technology development require specific code examples. In Vue technology development, asynchronous request processing is often encountered. Asynchronous requests mean that while sending a request, the program does not wait for the return result and continues to execute subsequent code. When processing asynchronous requests, we need to pay attention to some common issues, such as the order of processing requests, error handling, and concurrent execution in asynchronous requests. This article will combine specific code examples to introduce the asynchronous request processing problems encountered in Vue technology development and give

Vue is an extremely popular front-end framework, and Axios is currently a popular front-end asynchronous request library. This article will introduce in detail how to use Axios to send asynchronous requests in Vue. Installation and use of Axios Axios is a Promise-based HTTP client for sending asynchronous requests. We can install it via npm: npminstallaxios and then we can use it in Vue, first we need to import it in the component: importax

How to use Vue for asynchronous requests and data processing Vue.js is a component-based front-end development framework that simplifies the process of interacting with pages and provides rich functionality. In actual projects, we often need to obtain data from the server and process it accordingly. This article will introduce how to use Vue for asynchronous requests and data processing. Install axios When using Vue to make asynchronous requests, we usually use the axios library. First, we need to install axios in the project. OK

Send asynchronous HTTP requests and process responses using the new HttpClient in Java 11 In Java 11, the new HttpClient class was introduced, providing powerful functionality to send HTTP requests and process responses. Compared with the previous HttpURLConnection, the new HttpClient is easier to use and supports asynchronous operations, making it more efficient to handle concurrent requests. This article will introduce how to use the new HttpCli in Java11
