Home Common Problem How to get http status code in js

How to get http status code in js

Nov 17, 2023 pm 02:52 PM
js http status code

js can obtain http status code by using fetch function, fetch function and third-party library. Detailed introduction: 1. Use the XMLHttpRequest object to obtain the HTTP status code by calling the status attribute of the XMLHttpRequest object; 2. Use the fetch function to process the response through the then method, and obtain the HTTP status code through the status attribute of the response object; 3. , use third-party libraries, etc.

How to get http status code in js

The operating system of this tutorial: windows10 system, javascript2023 version, DELL G3 computer.

In JavaScript, you can obtain the HTTP status code in the following ways:

1. Use the XMLHttpRequest object:

In traditional JavaScript , you can use the XMLHttpRequest object to send HTTP requests and get responses. The HTTP status code can be obtained by calling the status attribute of the XMLHttpRequest object. For example:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://example.com/api', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log(xhr.status); // 输出200
}
};
xhr.send();
Copy after login

In the above code, the HTTP status code can be obtained through xhr.status.

2. Use the fetch function:

In modern JavaScript, you can use the fetch function to send HTTP requests and get responses. The fetch function returns a Promise object, which can process the response through the then method, and obtain the HTTP status code through the status attribute of the response object. For example:

fetch('http://example.com/api')
.then(function(response) {
if (response.ok) {
console.log(response.status); // 输出200
}
});
Copy after login

In the above code, the HTTP status code can be obtained through response.status.

3. Use third-party libraries:

In addition to native JavaScript methods, you can also use some third-party libraries to simplify the process of obtaining HTTP status codes. For example, using the Axios library:

axios.get('http://example.com/api')
.then(function(response) {
console.log(response.status); // 输出200
});
Copy after login

In the above code, the HTTP status code can be obtained through response.status.

It should be noted that the HTTP status code obtained by the above method is obtained asynchronously, because the HTTP request is an asynchronous operation. Therefore, you may need to wait for a while before getting the HTTP status code. Asynchronous operations can be handled through callback functions, Promise, async/await, etc. to ensure that subsequent operations are performed after obtaining the HTTP status code.

In addition, it should be noted that the HTTP status code obtained by the above method is the status code returned by the server, not the status code when the browser sends the request. The status code when the browser sends the request (such as 404 Not Found) can be viewed through the browser's developer tools (such as Chrome's developer tools).

The above is the detailed content of How to get http status code in js. 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

Video Face Swap

Video Face Swap

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

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)

Revealing the causes of HTTP status code 460 Revealing the causes of HTTP status code 460 Feb 19, 2024 pm 08:30 PM

Decrypting HTTP status code 460: Why does this error occur? Introduction: In daily network use, we often encounter various error prompts, including HTTP status codes. These status codes are a mechanism defined by the HTTP protocol to indicate the processing of a request. Among these status codes, there is a relatively rare error code, namely 460. This article will delve into this error code and explain why this error occurs. Definition of HTTP status code 460: First, we need to understand the basics of HTTP status code

A deep dive into the meaning and usage of HTTP status code 460 A deep dive into the meaning and usage of HTTP status code 460 Feb 18, 2024 pm 08:29 PM

In-depth analysis of the role and application scenarios of HTTP status code 460 HTTP status code is a very important part of web development and is used to indicate the communication status between the client and the server. Among them, HTTP status code 460 is a relatively special status code. This article will deeply analyze its role and application scenarios. Definition of HTTP status code 460 The specific definition of HTTP status code 460 is "ClientClosedRequest", which means that the client closes the request. This status code is mainly used to indicate

Analyze the causes and solutions of HTTP status code 460 Analyze the causes and solutions of HTTP status code 460 Feb 20, 2024 pm 02:03 PM

Explore the causes and solutions of HTTP status code 460. The HTTP status code is a standardized numeric code used by the HTTP protocol to indicate the result returned by a request. In the HTTP/1.1 specification, a total of 5 types of status codes are defined, among which 4xx status codes indicate request errors and 5xx status codes indicate server errors. Among these status codes, we have rarely heard of the 460 status code. So, what is HTTP status code 460? What is the reason for its appearance? How should we solve it? First, let’s look at the HTTP status codes

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Introduction to HTTP 525 status code: explore its definition and application Introduction to HTTP 525 status code: explore its definition and application Feb 18, 2024 pm 10:12 PM

Introduction to HTTP 525 status code: Understand its definition and usage HTTP (HypertextTransferProtocol) 525 status code means that an error occurred on the server during the SSL handshake, resulting in the inability to establish a secure connection. The server returns this status code when an error occurs during the Transport Layer Security (TLS) handshake. This status code falls into the server error category and usually indicates a server configuration or setup problem. When the client tries to connect to the server via HTTPS, the server has no

An in-depth analysis of HTTP status code 550: Bad email address An in-depth analysis of HTTP status code 550: Bad email address Feb 18, 2024 pm 01:44 PM

In-depth analysis of HTTP status code 550: Incorrect email address With the rapid development of the Internet, email has become an indispensable part of people's daily life and work. Through email, people can transfer information and communicate quickly and easily. However, in the process of using email, we sometimes encounter some problems, one of which is the wrong email address. When sending emails online, we often encounter situations where the email fails to be sent. When we receive something like “550Error:Invalid

How to create a stock candlestick chart using PHP and JS How to create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price