php - 返回错误,以便js承诺捕获
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):
<?php
session_start();
$_SESSION['user_id'] = 1;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Methods: GET,HEAD,OPTIONS,POST,PUT");
header("Access-Control-Allow-Headers: Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");
if ( $_SESSION['user_id'] == 0 ) {
header('Content-Type: application/json');
print json_encode(["test" => 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.
甚至可以从php文件中返回某种错误(即使用header()) ajax调用并被视为错误(通过catch方法?) p>
考虑这样的php(示例无法正常工作): p>
&lt; ?php
session_start();
$ _SESSION ['user_id'] = 1;
标题(“Access-Control-Allow-Origin:*”);
标题(“访问控制” -Allow-Credentials:true“);
header(”Access-Control-Allow-Methods:GET,HEAD,OPTIONS,POST,PUT“);
header(”Access-Control-Allow-Headers:Access-Control“ -Allow-Headers,Origin,Accept,X-Requested-With,Content-Type,Access-Control-Request-Method,Access-Control-Request-Headers“);
if($ _SESSION [' user_id'] == 0){
header('Content-Type:application / json');
print json_encode([“test”=&gt; 123]);
}
else
{
标题 ('HTTP / 1.1 1337内部服务器Kalreg');
标题('Content-Type:application / json; charset = UTF-8');
die(json_enco) de(array('message'=&gt; '错误','代码'=&gt; 1337));
}
?&gt;
code> pre>
和js: p>
axios.get ('loginStatus.php',{crossDomain:true})
.then(function(response){
console.log(response);
})
.catch(function(error){
console。 log(错误);
});
}
}
code> pre>
现在因为$ _SESSION ['user_id']设置为1 生成服务器错误,因为js中的catch应该以适当的方式做出反应。 实际上它不是 - 在chrome-dev-tools中我得到一个错误: p>
xhr.js?ec6c:178 OPTIONS
http://192.168.0.15/game/src/server/loginStatus.php 500(内部
服务器 错误)无法加载
http://192.168.0.15/game/src/server /loginStatus.php :对
预检的响应包含无效的HTTP状态代码500. p>
blockquote>
这可能吗? 或者我应该只返回错误的JSON文件并使用.then而不是.catch管理它? p>
Kalreg。 p>
div>
If you want catch to be executed and this is decided on server-side, then you will need to set an unsuccessful status code via http_response_code. A status code of 417 (Expectation failed) seems to be a good pick in your case, but you can see here what you can pick from.