我试过使用 ajax 调用我的 php 函数,但我不知道代码有什么问题,它不起作用
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 调用中键入 属性 定义了两次。
请使用调试工具,例如Firebug 来调试xhr 请求以了解它们是否被发送。您还可以查看那些可能提示错误的请求的响应。
在我将 javascript 文件的顺序更改为表单的 html 文件后,代码正常运行。
我将 javascript 代码文件放在
之后
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
而且它运行良好。
!-- 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 调用中键入 属性 定义了两次。
请使用调试工具,例如Firebug 来调试xhr 请求以了解它们是否被发送。您还可以查看那些可能提示错误的请求的响应。
在我将 javascript 文件的顺序更改为表单的 html 文件后,代码正常运行。
我将 javascript 代码文件放在
之后<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
而且它运行良好。