2017-07-06 117 views
0

這是我登錄控制器的核心腳本,這意味着所有的用戶清理和驗證都完成了。裏面LoginController.phpPhp重定向導致302重定向到了ajax

if (password_verify($valid['pass'], $pass)) { 
    // My session stuffs 

    header('Content-Type: application/json'); 
    echo json_encode (array(
     'result' => 'success', 
     'msg' => 'Login successful..! You will be redirected in a moment' 
    )); 
    redirect('user/profile'); //Ajax redirect only works when I remove this. 
    exit; 
} 

代碼

代碼中的login.php

try { 
    LoginController::UserLogin(); // Calling my method inside my controller 
} catch (Exception $e) { 

    echo json_encode(array(
     'error' => array(
      'msg' => $e->getMessage() 
     ), 
    ), JSON_PRETTY_PRINT); 
    header('Content-Type: application/json'); 
    exit; 

} 


<form id="login_form" action="<?php echo htmlspecialchars(pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME)); ?>" class="form" method="POST"> 
    <div class="form-group"> 
     <input type="text" name="login_username" placeholder="Enter username" class="form-control" /> 
    </div> 
    <div class="form-group"> 
     <input type="password" name="login_pass" placeholder="Enter password" class="form-control" /> 
    </div> 
    <div class="form-group"> 
     <input type="text" name="login_security" placeholder="Enter Security 25 * 2" class="form-control" /> 
    </div> 
    <button class="btn btn-green" id="login-btn">Login</button> 

</form> 

我的AJAX部分

$('#login_form #login-btn').on('click', function (e) { 
    e.preventDefault(); 

    $.ajax({ 
     url : $('#login_form').attr('action'), 
     type: 'POST', 
     dataType: 'json', 
     data: $('#login_form').serialize(), 
     success: function(data) { 
      console.log(data); 
      if (data.error) { 
       $('.error').html('<div class="alert alert-danger">' + data.error.msg + '</div>'); 
      } 
      if (data.result === 'success') { 
       $('.error').html('<div class="alert alert-unknown">' + data.msg + '</div>'); 
       setTimeout('window.location.href = "user/profile.php";',3000); 
      } 
     } 
    }); 
}); 

我的問題:直到我用ajax,一切工作正常。這意味着沒有Ajax的頁面顯示錯誤,一切正常。但是,當我使用AJAX錯誤返回,但當登錄細節正確(有效細節)頁面卡住(該頁面不重定向)。

當我檢查開發者控制檯時,我所看到的是這302用於登錄頁面,並且重定向沒有發生(但是,當我從登錄控制器窗體中移除標題時,它會起作用,這導致當用戶沒有重定向時禁用JavaScript)。

enter image description here

+0

你的意思'302',而不是'304'?如果是這樣,這是一個非常有效的迴應 –

+0

糟糕,這是一個302錯誤。問題是頁面沒有被重定向到ajax。 –

回答

0

試試這個,看看它是否工作:

在LoginController.php:去掉redirectecho部分之後。

在Login.php中,將header放在echo之前。

在阿賈克斯的一部分,土長success部分是這樣的:

success: function(data) { 
     console.log(data); 
     if (data.error) { 
      $('.error').html('<div class="alert alert-danger">' + data.error.msg + '</div>'); 
     } 
     if (data.result === 'success') { 
      $('.error').html('<div class="alert alert-unknown">' + data.msg + '</div>'); 
      // -> here 
      // correct setTimeout function 
      setTimeout(function(){ 
       window.location = "user/profile.php"; 
      },3000); 
     } 
    } 
+0

現在,ajax部分正常工作,但禁用JavaScript時會打印原始json。 –

+0

是的,Ajax和Jquery無法在沒有JavaScript的情況下工作。如果你希望你的頁面在沒有JS的情況下工作,你必須做如下改變:如果表單沒有js,發送一個類似「ajax = 0」的參數,如果js在工作,發送一個參數「ajax = 1」。在php文件中,你檢查「ajax」參數並決定要做什麼。 (發送Json或直接重定向) – verjas