Update 2024:
If you need a reliable (currently chrome based only) solution use https://stackoverflow.com/a/76918876/2590616 which is the best choice imho.
Alternatively
Include the status code on the server side, then read it on the client side via JavaScript, e.g.:
a) Java + Thymeleaf + meta tag:
<meta name="statuscode" th:content="${#response.status}">
<script>
const statusCode = document.getElementsByName("statuscode").getAttribute("content");
</script>
b) Java + Thymeleaf + JavaScript variable:
<script th:inline="javascript">
const statusCode = [[${#response.status}]];
</script>
c) PHP + meta tag (unverified):
<meta name="statuscode" content="<?php echo http_response_code() ?>">
<script>
const statusCode = document.getElementsByName("statuscode").getAttribute("content");
</script>