如何检查laravel中的ajax状态

时间:2022-05-25 07:18:14

i'm making app in laravel with register and login form. i have ajax login form. it works fine when user enters correct mail and pass, but if user enters incorrect mail or pass it redirect on login page. can anyone help me how to display error in login form without redirect?

我正在使用注册表和登录表单在laravel中制作应用程序。我有ajax登录表单。当用户输入正确的邮件并通过时,它可以正常工作,但如果用户输入错误的邮件或在登录页面上重定向它。谁能帮助我如何在没有重定向的情况下在登录表单中显示错误?

public function login(){
  $data = Request::all();
  if (Auth::attempt(['email' => $data['email'], 'password' => $data['password']]))
   {
       return redirect('/');
   }
  else {
      return redirect('login');
  }
}

this is my controller code

这是我的控制器代码

1 个解决方案

#1


2  

In the else part instead of

在else部分而不是

return redirect('login');

Use this

Session::flash('errorMessage', 'Authentication Failed');
return Redirect::back(); 

In the View (blade) add this

在视图(刀片)中添加此项

@if ($message = Session::get('errorMessage'))
  <div class="alert alert-danger alert-block">
  <button type="button" class="close" data-dismiss="alert">&times;</button>
  <h4>Authentication Error</h4>
     <span class="errorText">{{ $message }}</span>
  </div>
@endif

(Without refresh) Try this: In the else part add

(不刷新)试试这个:在else部分添加

return Response::json([
    'message' => 'Authentication Failed'
 ]);

In the Ajax success function, you can display this message from the return JSON. Place the message in the span with class "errorText".

在Ajax成功函数中,您可以从返回JSON中显示此消息。将消息放在带有“errorText”类的范围中。

In View:

  <div class="alert alert-danger alert-block hidden">
  <button type="button" class="close" data-dismiss="alert">&times;</button>
  <h4>Authentication Error</h4>
     <span class="errorText"></span>
  </div>

Remember to remove the class "hidden" using the jquery remove().

请记住使用jquery remove()删除“隐藏”类。

Note: Remember to include namespace Illuminate\Http\Request and Illuminate\Http\Response if necessary.

注意:如果需要,请记住包括命名空间Illuminate \ Http \ Request和Illuminate \ Http \ Response。

Hope this is helpful.

希望这有用。

#1


2  

In the else part instead of

在else部分而不是

return redirect('login');

Use this

Session::flash('errorMessage', 'Authentication Failed');
return Redirect::back(); 

In the View (blade) add this

在视图(刀片)中添加此项

@if ($message = Session::get('errorMessage'))
  <div class="alert alert-danger alert-block">
  <button type="button" class="close" data-dismiss="alert">&times;</button>
  <h4>Authentication Error</h4>
     <span class="errorText">{{ $message }}</span>
  </div>
@endif

(Without refresh) Try this: In the else part add

(不刷新)试试这个:在else部分添加

return Response::json([
    'message' => 'Authentication Failed'
 ]);

In the Ajax success function, you can display this message from the return JSON. Place the message in the span with class "errorText".

在Ajax成功函数中,您可以从返回JSON中显示此消息。将消息放在带有“errorText”类的范围中。

In View:

  <div class="alert alert-danger alert-block hidden">
  <button type="button" class="close" data-dismiss="alert">&times;</button>
  <h4>Authentication Error</h4>
     <span class="errorText"></span>
  </div>

Remember to remove the class "hidden" using the jquery remove().

请记住使用jquery remove()删除“隐藏”类。

Note: Remember to include namespace Illuminate\Http\Request and Illuminate\Http\Response if necessary.

注意:如果需要,请记住包括命名空间Illuminate \ Http \ Request和Illuminate \ Http \ Response。

Hope this is helpful.

希望这有用。