The status code is the status
property on the response object. Also, unless you’re using JSON with your error responses (which some people do, of course), you need to check the status code (or the ok
flag) before calling json
:
fetch(`${baseUrl}api/user/login`, {
credentials: "include", // ¹ See note below
headers: myHeaders
})
.then(function(response) {
console.log(response.status); // Will show you the status
if (!response.ok) {
throw new Error("HTTP status " + response.status);
}
return response.json();
})
.then(// ...
Not checking that the request succeeded is such a common mistake I wrote it up on my anemic old blog.
¹ You had withCredentials: true
, but the documentation says it’s credentials: "include"
. (Thank you aderchox for pointing that out.)