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'重定向 –