如果使用Ajax POST请求时出错,但是无错误信息返回,或导致浏览器JS脚本卡住无反应,可以这样解决:
将文中的自定义Http类这样改写:
public function render(Exception $e)
{
// 如果是Ajax请求时发生异常
if ( request()->isAjax() ) {
$errorStr = $e->getMessage() .'<br/>'.
$e->getFile() .' Line:'. $e->getLine();
return response($errorStr);
}
// 其他错误交给系统处理
return parent::render($e);
}
同时app设置中这样设置:
// 异常处理handle类 留空使用 \think\exception\Handle
'exception_handle' => '\app\common\exception\Http',
这样的话Ajax就会把详细的错误信息错误文件及行数返回给浏览器。