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