Problems with Ajax asynchronous requests in the XMLHttpRequest object
XMLHttpRequestObject
1. XMLHttpRequest object
1.Ajax can realize asynchronous transmission, so It relies on XMLHttpRequest in JavaScript
2. The XMLHttpRequest object is the object of the XMLHttp component. It is an abstract object that allows the script to obtain the returned eXML data from the server or send the data to the server.
3.XMLHttpRequest can realize only data-level interaction between the client and the server without having to refresh the page every time
4.XMLHttpRequest was first provided as an ActiveX control in Microsoft Internet Explorer 5.0 and has since been widely used
5.You need to create an XMLHttpRequest object before using XMLHttpRequest to send a request and process the response.
6.XMLHttpRequest is not a W3C standard and can be created using JavaScript in a variety of ways. XMLHttpRequest instance
7.XMLHttpRequest is implemented as an ActiveX control in IE, while other browsers implement it as a JavaScript built-in object
2. XMLHttpRequest object creation
3. XMLHttpRequest object properties (receive and display the current status)
1.readySate- Logging returns the status of the request
. 0-for initialization: the object has been created, the unit has been initialized, and the open method has not been called;
. 1-Initialization: The object has been created, but the send method has not been called to send the request;
. 2-Send data: The send method has been called, but the HTTP header is unknown;
. 3-Data transmission: Part of the data has been accepted, but the response is incomplete;
. 4-Complete: The data acceptance is completed, and only then can the complete return data be obtained
2.responseText-Receive the text content of the client's HTTP response
. Read only
. When readySate is 1/2, the responseText value is an empty string;
. When readyState is 3, the response information is being received and has not been completed;
. When readyState is 4, it means that the response information has been received
. xmlHttp default response data encoding is UTF-8
3.responseXML-After send() is executed, the returned information is formatted into an XML Document object
. The MIME type specified by Content-Type should be text/HTML
. If Content-Type does not contain this type, responseXML will get a null value when receiving
4.status-After send() is executed, status can be used to read the status of things
. Long integer data
. Returns the HTTP status code
of the current request. This attribute is only used when readyState is 3 or 4, otherwise an error
will occur when reading the status. 100 - The client must continue sending requests
. 200-Transaction successful
. 400 - Bad request
. 403 - Request not allowed
. 404-File, query, URL
not found. 500-Server Internal Error
. 502 The server is temporarily unavailable
. 505-The server does not support or refuses to support the HTTP version in the request header
5. After statusText-send() is executed, the status of the thing can be read through statusText
. Returns the status line
of the current HTTP request. This attribute can only be used when readyState is 3 or 4, otherwise an error will occur when reading state
6.Onreadystatechange-The operation to be performed when readyState changes
. Usually the handler function name is assigned to onreadystatechange to specify event handling
for the XMLHttpRequest object. In the event processing function, perform corresponding processing
based on the status value ofreadyState. Example:
function test(){ xmlHttp.onreadystatechange=showInfo; var url=”/ajax/urlInfo”;//请求路径 xmlHttp.open(“GET”,url,true); xmlHttp.send(null); } Function showInfo(){ If(xmlHttp.readyState==4){ alert(“success”); } }
4. XMLHttpRequest object method (dynamic processing of various information: sending and receiving data, processing of requests and responses, etc.)
1.abort()-terminate the current operation
. Stop the XMLHttpRequest object's HTTP request and restore the object to its initial state
2.open()-xmlHttp.open(method,url,mode,user,pwd)
. Create a new HTTP request and specify the request method, URL, verification information, etc.
. method: POST, GET, PUT (case can be ignored)
. url: The requested target address
. mode: Specifies whether the request is asynchronous, the default is true; when true, when the state changes, the processing function specified by the onreadystatechange attribute
will be called. After calling open(), the XMLHttpRequest object sets the readyState attribute to 1 and restores the initial values of responseText, responseXML, status, statusText and other attributes, and resets the request header information
Call ## When #open(), readyState is 4, the XMLHttpRequest object will reset the above value
3.send()-xmlHttp.send(content). Send a request to the server and receive the response4.setRequestHeader()-setRequestHeader(header, value). Separately set the HTTP header information
of a request. When readyState is 1, this method can be called after send(), otherwise an exception
is returned. If a HTTP header with this name already exists, the original information will be overwritten
. header-header name: string type
. value-The value of the header name: string type
5.getResponseHeader()-Read the header of the information sent by the server. HEAD requests ignore content, so their responses are smaller than responses to GET or POST
Get content:. Content-Type: Content type
. Content-Length: Content length
. Last-Modify: The date of the last modification
. Example: function getHeadInfo(){
if(xmlHttp.readyState==4){ if(headeyType==”Content-Type”){ window.alert(“Content-Type:”+xmlHttp.getResponseHeader(“Content-Type”); } else if(headType==”Content-Length”){ window.alert(“Content-Length:”+xmlHttp,getResponseHeader(“Content-Length”); } else if(headType==”Last-Modify”){ window.alert(“Last-Modify:”+xmlHttp.getResponseHeader); } } }. When obtaining header information, not all information can be obtained6.getAllResponseHeaders()-Get all header information
。在获取时只用HEAD即可获取
。例:fuction headRequest(){
creatXMLHttpRequest(); xmlHttp.onreadystatechange=getHeadInfo; xmlHttp.open(“HEAD”,”url”,false); xmlHttp.send(null); } function getHeadInfo(){ if(readyState==4){ Alter(xmlHttp.getAllResponseHeaders()); }
The above is the detailed content of Problems with Ajax asynchronous requests in the XMLHttpRequest object. For more information, please follow other related articles on the PHP Chinese website!

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

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


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 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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