search
HomeCommon ProblemHow to get http status code in js

How to get http status code in js

Nov 17, 2023 pm 02:52 PM
jshttp 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();

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
}
});

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
});

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool