![](/img/trans.png)
[英]I dont know whats wrong with this simple javascript code, I have checked this over and over again but does not work
[英]I have tried using ajax to call into my php function but i dont know whats wrong with the code its not working
!-- Main Page Starts Here -->
<section class="container">
<div class="row">
<div class="centerlogin">
<div class="frmlogin">
<form role="form" name="signin" id="signin" method="post" action="#">
<div class="headtab"><h3>Login</h3></div>
<ul>
<li><i class="glyphicon glyphicon-user"></i> <input type="text" id="email" name="username" class="usern" placeholder="Enter Username"></li>
<li><i class="glyphicon glyphicon-lock"> </i><input type="password" id="pwd" name="password" class="passn" placeholder="Enter Password"></li>
<li><button class="subn" id="btnSubmit">Login</button></li>
</ul>
</form>
</div>
</div>
</div>
</section>
<!-- Main Page Ends Here -->
以上是我的登錄表格。
以下是我的ajax電話
//ajax calls start below $(document).ready(function () { $("#btnSubmit").click(function (e) { e.preventDefault(); var email = $("#email").val(); var password = $("#pwd").val(); var pwd = $.md5(password); auth(email, pwd); }); }); //authenticate function to make ajax call function auth(email, pwd) { $.ajax ({ type: "POST", url: "https://localhost/main/web/sign-in", dataType: 'json', type : "POST", data: { email: email,pwd: pwd }, success: function (r) { //console.log(r); if(r.status == '0') { var sk=r.sk; $.ajax({ type: "POST", url: "http://localhost/main/secret/signin.php", type : "POST", data: { sk:sk}, success: function(r) { if(r == '0') { window.location.href = "http://localhost/main/index.php"; } else { window.location.href = "http://localhost/main/login.php"; alert('Something Went Wrong.Please Try Again!'); } } }); } else if(r.status == '401') { alert("Incorrect Email/Password"); $("#signin")[0].reset(); } else { alert("User Doesn't exist"); $("#signin")[0].reset(); } return false; } }); }
我不知道我的代碼是什么錯,即使代碼不能正常工作,它甚至沒有顯示表單空白輸入上的警報,單擊登錄按鈕后表單重新加載,請幫助我卡住非常糟糕。
嘗試下面的代碼,因為我剛從url中刪除了http://。 希望這可以幫助。
//ajax calls start below
$(document).ready(function () {
$("#btnSubmit").click(function (e) {
e.preventDefault();
var email = $("#email").val();
var password = $("#pwd").val();
var pwd = $.md5(password);
auth(email, pwd);
});
});
//authenticate function to make ajax call
function auth(email, pwd) {
$.ajax
({
type: "POST",
url: "web/sign-in",
dataType: 'json',
type : "POST",
data: { email: email,pwd: pwd },
success: function (r) {
//console.log(r);
if(r.status == '0')
{
var sk=r.sk;
$.ajax({
type: "POST",
url: "secret/signin.php",
type : "POST",
data: { sk:sk},
success: function(r)
{
if(r == '0')
{
window.location.href = "main/index.php";
}
else
{
window.location.href = "main/login.php";
alert('Something Went Wrong.Please Try Again!');
}
}
});
}
else if(r.status == '401')
{
alert("Incorrect Email/Password");
$("#signin")[0].reset();
}
else
{
alert("User Doesn't exist");
$("#signin")[0].reset();
}
return false;
}
});
}
Ajax調用中的Type屬性定義了兩次。
請使用調試工具(如Firebug)來調試xhr請求,以了解它們是否被發送。 您還可以查看可能提示錯誤的請求的響應。
在我將javascript文件的序列更改為我的表單的html文件后,代碼正在運行。
我把我的javascript代碼文件放在后面
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
它工作得很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.