2015-05-22 51 views
0

我正在使用簡單的登錄頁面(html/css/ajax)登錄到用戶。 我有一些我想做的事情,並會欣賞投入。無法登錄/等待消息和重定向html/css/ajax

1)如果未經授權,您如何使未登錄的消息失效?我想讓它只是說未經授權的,而不是硬彈出。

2)同樣的事情等待消息。我如何提醒用戶在重定向之前等待5秒鐘左右(我希望消息最好彈出)? 我使用window.location = url作爲用戶登錄後立即重定向。 此外,有沒有辦法在生產中隱藏用戶的window.location?

還有一個簡單的帶有用戶名/密碼的html頁面。

<script type="text/javascript"> 
    $('form').submit(function(){ 
        $.ajax({ 
          type: "Post", 
          url: $('form').attr('action'), 
          data: $(this).serialize(), 
          success: function (result) { 
          window.location = 'http://login/user/login/'; 
          } 
          }); 
              return false; 
        }); 
    </script> 

</body> 
</html> 
+0

請發佈您的代碼 - 有很多不同的方法可以完成。 – user2182349

+0

@ user2182349發佈。你可以看一下嗎? – user17651

回答

0

1)可能是你要等待,您可以使用setTimeout功能注入HTML

2)。第二部分,您不能隱藏JavaScript函數,因爲它們在客戶端運行,客戶端需要下載運行源代碼。

編輯: HTML

<p id="my-message-for-login">my login message</p> 

的JavaScript

0123:

簡單,你可以通過HTML DOM

<p id="demo" onclick="myFunction()">Click me to change my HTML content (innerHTML).</p> 

function myFunction() { 
    setTimeout(function() { 
     document.getElementById("demo").innerHTML = "Paragraph changed!"; 
    }, 2000); 
} 

DEMO

EDIT 2做

$.ajax({ 
    type: "post", 
    url: $('form').attr('action'), 
    data: $(this).serialize(), 
    success: function (result) { 
     if(result == 'isLoggedSuccessfuly') { 
      $("#my-message-for-login").text("You are logged and you will be redirected in 2 seconds"); 
      setTimeout(function() { 
       window.location.href = '/you/are/logged/page'; 
      }, 2000); 
     } 
     else { 
      $("#my-message-for-login").text("Something wrong! You are NOT logged"); 
     } 
    } 
}); 
+0

有道理。我沒有setTimeout,但你可以顯示一個例子,爲失敗的登錄信息注入html和等待消息? – user17651

+0

雖然這是什麼HTML DOM?這是消息嗎?它只是說點擊,它會改變,但是這是一個等待消息或失敗? – user17651

+0

你想要什麼?用戶登錄你想要顯示消息並在5秒後重定向他? – daremachine

0

用一個簡單的登錄表單

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Login</title> 
<link rel="stylesheet" type="text/css" href="css/style.css"> 
</head> 
<body> 
<div id="login"> 
<h1>Login</h1> 
<p id="login-error-message" class="error" style="display:none">Please enter a username and password</p> 
<label for="username">Username</label> 
<input type="text" id="username" name="username" required> 
<label for="password">Password</label> 
<input type="password" id="password" name="password" required> 
<button id="login-btn">Login</button> 
</div> 

您可以使用此jQuery代碼:如果證書服務器接受它可能返回

$("#login-btn").on("click", function (evt) { 
    $("#login-error-message").hide(); 
    $.ajax({ 
     method: "post", 
     url: "auth.php", 
     data: { 
      "username": $("#username").val(), 
       "password": $("#password").val() 
     } 
    }).done(function (data, statusText, xhr) { 
     if (xhr.status === 200 || xhr.status === 204) { 
      setTimeout(function() { 
       location.href = "index.php"; 
      }, 5000); 
     } 
    }); 
    $("#login-error-message").show(); 
}); 

一個200或204,然後等待五秒和重定向。

如果不重定向,則會顯示登錄錯誤消息。