javascript任何类型的重定向都不起作用

时间:2016-01-14 02:45:06

标签: jquery ajax

我有一个简单的javascript,用于检查用户是否输入了正确的用户名和密码,并被重定向到管理页面,但它不起作用。这是我的代码:

$(document).ready(function(){
$('#login').click(function(){
var username = $('#user').val();
var password = $('#pass').val();

if(username == "")
{
  $('#dis').slideDown().html("<span>Please type username</span>");
  return false;
}

if(password == "")
{
  $('#dis').slideDown().html("<span id='error'>Please type password</span>");
  return false;
}

if(username == "admin" && password == "admin")
{
  window.location("registrar.html");
}
else
{
  alert("Incorrect Username or Password");
}
});
});

我已经尝试了不同的方法,但它仍然无法正常工作.. 我还尝试将页面的完整地址http://localhost/folder/registrar.html放在一起但不起作用......

3 个答案:

答案 0 :(得分:0)

替换:

window.location("registrar.html");

使用:

window.location.href = "registrar.html";

答案 1 :(得分:0)

这是我出来的解决方案。我添加这个PHP

session_start();

$error = $user = $pass = "";
$check = "";

if(!empty($_GET['user']) && !empty($_GET['pass']))
{
$user = htmlentities($_GET['user']);
$pass = htmlentities($_GET['pass']);

if($user == "admin" && $pass == "admin")
{
    $_SESSION['user'] = $user;
    $_SESSION['pass'] = $pass;

    header("Location: registrar.html");
   }
else
{
    echo "<script>alert('Incorrect Username or Password');
    window.location.href = 'login.html';</script>";
}
}

并将此代码移至javascript:

if(username == "admin" && password == "admin")
{
  window.location = "registrar.html";
}
else
{
  alert("Incorrect Username or Password");
}

答案 2 :(得分:0)

// same as an HTTP redirect
window.location.replace("registrar.html");

// same as clicking on a link
window.location.href = "registrar.html";