Implement ajax to send asynchronous requests (graphic tutorial)
This article mainly teaches you how to easily implement ajax to send asynchronous requests, which has certain reference value. Interested friends can refer to
ajax sends asynchronous requests for your reference. The specific content is as follows
First step(Get XMLHttpRequest)
Ajax actually only needs to learn one object: XMLHttpRequest. If you master it, you will master ajax!!!
1. Get XMLHttpRequest
Most browsers support: var xmlHttp=new XMLHttpRequest();
IE6.0:var xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
IE5.0 uses earlier versions of IE: var xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
2. Write a function that creates an XMLHttpRequest object
function createXMLHttpRequest(){ try{ return new XMLHttpRequest(); } catch(e){ try{ return new ActiveXObject(“Msxml2.XMLHTTP”); }catch(e){ try{ return new ActiveXObject(“Microsoft.XMLHTTP”); }catch(e){ alert(“哥们儿,你用的是什么浏览器啊?”); throw e; } } } }
The second step(Open the connection with the server)
xmlHttp.open(): used to open the connection with the server, it requires Three parameters:
Request method: can be GET or POST
Requested URL: Specify the server-side resource, for example:/day23_1/AServlet
Whether the request is asynchronous: If true, it means sending asynchronously request, otherwise synchronous request
xmlHttp.open("GET","/day23_1/AServlet",true);//For example
Step Three(Send Request)
xmlHttp.send(null): If not given, some browsers may not be able to send!
Parameters: Yes Request body content! If it is a GET request, null must be given.
If it is a POST request, as follows
xmlHttp.send(“username=zhangSan&password=123”);
Four steps:
Register a listener on an event of the xmlHttp object: onreadystatechange
The xmlHttp object has a total of 5 states
0: Initialization is not completed, just created XMLHttpRequest object, the open() method has not been called yet
1: The request has started, the open() method has been called, but the send() method has not been called yet
2: The request is sent in completion status, the send() method has been called
3: Start reading the server response
4: End reading the server response (usually we only care about the last status!!!)
Get the status of the xmlHttp object
var state = xmlHttp.readyState;//可能是0、1、2、3、4
Get the status code of the server response (200: Success 304: Status has not changed 404 500: Server error)
var status=xmlHttp.status;//例如200、404、500
Get the content of the server response
var content=xmlHttp.responseText;//得到服务器的响应的文本格式的内容(这更通用) var content=xmlHttp.responseXML;//得到服务器的响应的xml响应的内容,它是document对象了!
So the listener should be written like this
xmlHttp.onreadystatechange = function(){ //xmlHttp的5种状态都会调用本方法 if(xmlHttp.readyState ==4 && xmlHttp.status == 200){ //双重判断:判断是否为4状态,而且还要判断是否为200 var text=xmlHttp.responseText; } };
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Ajax implementation of saving and reading uploaded images to the background (graphic tutorial)
Struts2 and Ajax data interaction (graphic tutorial)
The above is the detailed content of Implement ajax to send asynchronous requests (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
