我已经尝试使用ajax来调用我的php函数,但我不知道它的代码是不正常的

时间:2017-02-16 07:52:35

标签: javascript php jquery ajax ajaxform

 !-- 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>&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" id="email" name="username" class="usern" placeholder="Enter Username"></li>
          <li><i class="glyphicon glyphicon-lock">&nbsp;</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电话

&#13;
&#13;
//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;
        }

        
      });
    } 
&#13;
   
&#13;
&#13;
&#13;

我不知道我的代码有什么问题,即使代码不能正常工作,它甚至没有显示表单空白输入上的提醒,并且单击登录按钮后表单会重新加载,请帮助我卡住非常糟糕。

3 个答案:

答案 0 :(得分:0)

尝试下面的代码,因为我刚从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;
        }


      });
    }

答案 1 :(得分:0)

Ajax调用中的Type属性定义了两次。

请使用调试工具(如Firebug)来调试xhr请求,以了解它们是否正在发送。您还可以查看可能提示错误的请求的响应。

答案 2 :(得分:0)

在我将javascript文件的序列更改为我的表单的html文件后,代码正在运行。

我将我的javascript代码文件放在

之后
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

并且它运作良好。

相关问题