ThinkPHP 5.1如果使用Ajax POST请求时出错,可以这样解决
2023-03-29 23:03:03
168
{{single.collect_count}}

如果使用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就会把详细的错误信息错误文件及行数返回给浏览器。

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}