2017-03-07 27 views
-1

我試着使用AJAX登錄到我的Web應用程序,但是當我提交登錄表單時,我得到一個空白頁面與JSON響應,例如成功登錄我得到:與JSON結果的空白頁面與AJAX的形式提交後

{"success":true} 

在錯誤的憑據,我得到:

{"fail":true,"errors":"Email or password is incorrect"} 

而且在錯誤的電子郵件或密碼,我得到(驗證錯誤):

{"fail":true,"errors":{"email":["The email field is required."],"password":["The password field is required."]}} 

我的js文件包含這段代碼:

$('#loginForm').submit('click',function(e){ 
    $.ajaxSetup({ 
     headers: { 
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') 
     } 
    }) 

e.preventDefault(); 

var formData = $(this).serialize(); 

$.ajax({ 
    type: "POST", 
    url: $(this).attr("action"), 
    dataType: 'json', 
    data: formData, 
    success: function (data) { 
     window.location.replace("localhost:8000"); 
    }, 
    error: function (data) { 
     $.each(data.errors, function(index,value) { 
      var errorDiv = '#'+index+'_error'; 
      $(errorDiv).html(value); 
     }); 
    } 
}); 
}); 

在我的控制,我有登錄功能:

public function login(Request $request){ 
     try{ 
      $validate = Validator::make($request->all(),[ 
       'email' => 'required|max:320', 
       'password' => 'required|max:150', 
       'remember_me' => 'nullable|accepted' 
      ]); 

     if($validate->fails()){ 
      return response()->json(array(
       'fail' => true, 
       'errors' => $validate->getMessageBag()->toArray() 
      )); 
     } 

     $rememberMe = $request->remember_me ? true : false; 

     if(Sentinel::authenticate(['username'=>$request->email, 'password' => $request->password], $rememberMe)){ 
      return response()->json(array(
       'success' => true 
      )); 
     } 
     elseif(Sentinel::authenticate(['email'=>$request->email, 'password' => $request->password], $rememberMe)){ 
      return response()->json(array(
       'success' => true 
      )); 
     } 
     else{ 
      return response()->json(array(
       'fail' => true, 
       'errors' => 'Email or password is incorrect' 
      )); 
     } 
    } 
    catch(ThrottlingException $e) { 
     $delay = $e->getDelay(); 

     return redirect('/')->with('error',"After too many login attempts\n you've banned for $delay seconds!"); 
    } 
    catch(NotActivatedException $e) { 
     return redirect('/')->with('error','Your account is not activated!'); 
    } 
    catch(Exception $e){ 
     return redirect('/')->with('error',$e->getMessage()); 
    } 
} 

形式:

{{ FORM::open(['url' => 'loginua','id'=>'loginForm']) }} 
<div class="input-container"> 
    {{ FORM::text('email', null, ['placeholder' => 'E-mail or Username','id'=>'email', 'autofocus' => 'autofocus','required' => 'required']) }} 
    <i class="fa fa-envelope-o"></i> 
    <div id="#email_error"></div> 
</div> 
<div class="input-container"> 
    {{ FORM::password('password', ['placeholder' => 'Password','id' => 'password', 'required' => 'required']) }} 
    <i class="fa fa-unlock"></i> 
    <div id="#email_error"></div> 
</div> 
<div class="input-container"> 
    <label> 
     <span class="radio"> 
      {{ FORM::checkbox('remember_me')}} 
      <span class="radio-value" aria-hidden="true"></span> 
     </span> 
     <span>Remember me</span> 
    </label> 
</div> 
<div class="input-container"> 
    {{ FORM::submit('Sign in',['class' => 'btn-style', 'name' => 'sign_in','id'=>'sign_in']) }} 
</div> 
<div class="user-box-footer"> 
    <p>Dont have account?<br><a href="{{url('register')}}">Sign up as User</a></p> 
    <p><a href="{{url('recovery')}}">Lost password?</a></p> 
    <p id="0_error"></p> 
</div> 
{{ FORM::close() }} 

路線:

Route::post('loginua', '[email protected]'); 

我不知道爲什麼成功或失敗的jQuery沒有觸發。提前致謝!

+0

請添加您的路線。 – hassan

回答

0

的問題是與視圖文件的元標記,我不得不添加此行的代碼對應於ajaxSetup:

<meta name="csrf-token" content="{{csrf_token()}}"/> 

謝謝大家提示。

0

使用

$('#loginForm').on('submit',function(e){ 

,而不是

$('#loginForm').submit('click',function(e){ 
+0

沒有變化,結果相同。 –

+0

使用url:'/ loginua',在ajax調用並從Form :: open()中移除'url'=>'loginua' –

+0

在FORM :: open()處沒有url我得到MethodNotAllowedHttpException。 –