Is that even possible to return some kind of error (ie using header()) from php file that is received by ajax call and reveived as error (via catch method?) consider such a php (example doesnt work correctly): 123]); } else { header('HTTP/1.1 1337 Internal Server Kalreg'); header('Content-Type: application/json; charset=UTF-8'); die(json_encode(array('message' => 'ERROR', 'code' => 1337))); } ?> and js: axios.get('loginStatus.php', { crossDomain: true }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); } } Now because $_SESSION['user_id'] is set to 1 server error is generated, and because of that catch in js should react in proper way. In fact it is not - in chrome-dev-tools i get an error: xhr.js?ec6c:178 OPTIONS http://192.168.0.15/game/src/server/loginStatus.php 500 (Internal Server Error) Failed to load http://192.168.0.15/game/src/server/loginStatus.php: Response for preflight has invalid HTTP status code 500. Is that possible? Or should i return just JSON file with error or not and manage it with .then not .catch? Kalreg.

如果你想执行 catch 并且这是在服务器端决定的,那么你将需要通过http_response_code设置一个不成功的状态代码。状态代码 417(预期失败)在您的情况下似乎是一个不错的选择,但您可以在此处看到可以选择的内容。

这有效,但是我仍然在控制台中收到错误,尽管执行了 catch。所以我执行了 catch 并且控制台充满了错误响应:2xhr.js?ec6c:178 OPTIONS 192.168.0.15/game/src/server/loginStatus.php 417 (Expectation Failed) Failed to load 192.168.0.15/game/src /server/loginStatus.php:预检响应具有无效的 HTTP 状态代码 417。这很烦人,因为我收到错误,但我确实想收到此错误,因为用户未登录。有什么解决办法吗?

@Kalreg 如果这是一个问题,您可以在 catch 中调用 clear() ,这将清除控制台中的错误消息。