好,所以我想提交一个登录页面而不用重定向。在阅读了关于这个的东西之后,我尝试了AJAX。所以我使用ajax观看了一些关于登录页面的教程。如何使用ajax读取json文件?
我引用这个视频:https://www.youtube.com/watch?v=MkGUL7ZRCTA
我想要做的是,用户需要先登录。但我不希望页面被重定向。只需检查密码是否与json文件中的密码匹配。但是当我尝试它时,控制台日志中没有显示任何内容(我正在使用Firefox)。
我想做的事情是这样的:但http://susheel61.0fees.net/ajaxtest.php 而不是显示其下的用户输入,它会显示“密码正确”或“密码不正确”(检查匹配后)
这里是我的代码摘要: HTML
<!DOCTYPE html>
<html>
<head>
<title> AJAX Tutorial</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<style type="text/css">
#loginbox{
width: 240px;
height: 140px;
margin: 0px auto;
margin-top: 300px;
}
</style>
</head>
<body>
<div id="loginbox">
<input id="password" type="password" placeholder="Enter Password" />
<button> Submit </button>
</div>
<div id="container"></div>
<script type="text/javascript">
$("button").click(function(){
var password = $("#password").val();
$.post("server.php", {password:password}, function(data){
console.log(data);
});
});
</script>
</body>
</html>
PHP
<?php
if (isset($_POST["password"])){
$password = $_POST["password"];
$jsondata = file_get_contents("CONFIG.json");
$json = json_decode($jsondata, true);
if ($password == $json["password"]) {
echo "Welcome";
} else {
echo "Wrong Password";
}
}
?>
和我的JSON
{"reset":"12312","sync":"232131","config":"2","bypass":"22","password":"qwerty"}
编辑:我试过用windrunn3r的答案。一切正常。但是,我希望如果密码正确,它会显示(隐藏)表单。这是我的新HTML。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> AJAX Tutorial</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<style type="text/css">
#loginbox{
width: 240px;
height: 140px;
margin: 0px auto;
margin-top: 300px;
}
</style>
</head>
<body>
<div id="loginbox">
<div id="message"></div>
<input id="password" type="password" placeholder="Current Password" />
<button> Submit </button>
<div id="container"></div>
</div>
<div id="newpass" style="display: none">
<input id="password" type="password" placeholder="New Password" />
<button> Submit </button>
</div>
<script type="text/javascript">
$("button").click(function(){
var password = $("#password").val();
$.ajax({
url: 'server.php',
cache: false,
async: true,
beforeSend: function() {
$("#message").html("Checking...");
console.log(password)
},
type: "POST",
data: { password: password }, //data to post to server
error: function (data) {
$("#message").hide();
$("#container").html("No password was sent");
},
success: function (data) {
$("#message").hide();
console.log(data)
if (console.log(data)=="Welcome"){
$("#newpass").show();
} else {
$("#container").html("Password Incorrect");
}
}
});
});
/*$("button").click(function(){
var password = $("#password").val();
$.post("server.php", {password:password}, function(data){
console.log(data);
if (console.log(data) == "Welcome") {
document.getElementById("container").innerHTML = "Password Correct!";
} else {
document.getElementById("container").innerHTML = "Password Incorrect!";
}
});
});*/
</script>
的可能的复制[如何解析与jQuery/JavaScript的JSON数据?](http://stackoverflow.com/questions/8951810/how-to-parse-json- data-with-jquery-javascript) – aldrin27
我不知道它是否有重复,但当我在我的控制台上添加和时,我能够在控制台上显示某些内容HTML文件 – gooey