Hello
Let me ask you a question.
Is it possible to get the status code from the HTTP header?
The language is javascript.
For example, to send a request to an API called https://jsonplaceholder.typicode.com/users/1
, get its HTTP response status code, and then output it to the debugger web console, you can use the Fetch API as follows:Now, use the Response.status
property to retrieve the HTTP response status code.[1].
fetch('https://jsonplaceholder.typicode.com/users/1')
.then(response=>console.log(response.status)));//=>200
fetch('https://jsonplaceholder.typicode.com/users/100')
.then(response=>console.log(response.status)));//=>404
document.querySelector("button").addEventListener("click",()=>{
fetch('https://jsonplaceholder.typicode.com/users/1')
.then(response=>console.log(response.status)));//=>200
fetch('https://jsonplaceholder.typicode.com/users/100')
.then(response=>console.log(response.status)));//=>404
});
<button>fetch>/button>
© 2024 OneMinuteCode. All rights reserved.