2
我喜欢用ajax插入数据库并取回来显示它。 我有这样laravel中间件不能在ajax请求url中工作
$.ajax({
method: 'POST',
url: "{{ route('comments.store') }}",
data: {
comment: 1,
comment_text: comment_text,
post_id: post_id,
"_token": "{{ csrf_token() }}"
});
和地址的路由(阿贾克斯comments.store)是中间件保护我的控制器
public function __construct()
{
$this->middleware('auth')->except(['index']);
}
public function store(Request $request) {
if(Auth::check()){
if (isset($request->comment)) {
$comment = new Comment([
'comment' => $request->comment_text,
'user_id' => Auth::id(),
'post_id' => $request->post_id
]);
$comment->save();
$comments = Comment::find($comment->id);
return response($comments);
}
}
return redirect()->route('home.post.show', $request->post_id);
}
('权威性') 当用户登录它的工作正常和响应$评论行。
但是当用户没有登录时,中间件不会将用户重定向到日志页面,并且它的响应错误是“未经验证的”。
如何解决它需要帮助。谢谢你..
什么是中间件的代码 –
这是Ajax请求用户登录页面。哪些不会自动重定向到任何页面。您需要成功检查条件,然后使用'window.location'重定向 –