laravel - 控制器的视图中没有显示错误消息
问题描述
我正在尝试显示错误消息,但未显示。我可以在控制台(网络选项卡)中看到错误消息,但视图中没有任何内容。提前致谢。
API 的输出。
{"message":"The given data was invalid.","errors":{"title":["The title field is required."]}}
控制器:
public function store(Request $request)
{
$request->validate([
"title"=> "required"
]);
try {
if ($request->ajax()) {
$request->merge(['created_by_user_info_id' => Auth::user()->id]);
SavingTypes::create($request->all());
$data['status'] = true;
$data['title'] = null;
$data['message'] = 'Added Successfully.';
}
} catch (\Exception $e) {
$data['status'] = false;
$data['message'] = 'Something went wrong. please try again';
$data['error'] = $e->getMessage();
}
return $data;
}
看法:
<h1>{{ $errors->first('title') }}test</h1> // only the "test" is visible and theres no error msg although I can see the error msg in the console (Network tab)
<input type="text" class="form-control" placeholder="title" name="title" >
解决方案
如果请求通过 AJAX 发送,那么您将无法访问
$errors->first('title')
您将在 JavaScript 中遇到错误
$.ajax({
// some code
error: function(jqXHR, textStatus, errorThrown) {
// Here you will get errors
console.log(textStatus, errorThrown);
}
});
推荐阅读
- python - Beautifulsoup 选择方法未按预期选择结果
- excel - Excel VBA 字典数据完整性问题
- docusignapi - 用于处理已处理的docusign doc 的Docusign API 请求
- excel - VBA 编译错误:错误数量的参数或无效的属性分配尝试取消合并多个工作表上的 excel 单元格时
- reactjs - 找不到模块:无法解析“./reducers/index”
- python - 在 python 中使用来自不同容器的 Kafka 消息
- php - 如何在php中从网络驱动器读取文件?
- r - 如何处理R中函数列表的评估
- javascript - 如何获得相对于背景img的鼠标位置
- javascript - 如何使用渲染之间的挂钩从其父组件调用渲染道具提供的回调