我想要的是返回的xmlhttp.status是自定义的状态码,请问如何实现。
php接受到请求后只能return, 但是return的状态码总是200,http_response_code和header都不能实现,请问有其他办法吗?
下面是代码:
if ($validator->fails()) { //检查是否出错
$error = $validator->errors()->all(); //获取错误,后面就是返回错误了,如何实现?
header('Content-Type:application/json; charset=utf-8');
exit(json_encode(['code'=>418,'msg'=>$error[0]],true));
}
首先是判断表单输入是否合规则,如果不合规则,就返回错误。
用过的方法:
http_response_code(xxx);
return string;
------
http_response_code('xxx');
return string;
------
header('HTTP/1.1 404 Not Found');
return string;
它们,都不行,前台xmlhttp.status检测总是200。
难道说...这个return不行吗?要用echo?
好吧,改为exit()就行了,return不行,谢谢各位了!
http_response_code(418);
exit($error[0]);
是说 http 返回的状态码?还是成功返回的 json 里的某个字段 ?
如果是 http 状态码,如果成功了,就应该是 200 。不建议修改其他的。
而且,你说的
http_response_code
和header
应该都是可行的。建议你贴出代码看看