【问题标题】:I have tried using ajax to call into my php function but i dont know whats wrong with the code its not working我尝试使用 ajax 调用我的 php 函数,但我不知道代码有什么问题,它不起作用
【发布时间】:2017-02-16 07:52:34
【问题描述】:
 !-- 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调用

//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;
        }

        
      });
    } 
   

我不知道我的代码有什么问题,即使代码不起作用,它甚至没有显示表单空白输入的警报,并且在单击登录按钮后表单会重新加载,请帮助我陷入困境。

【问题讨论】:

标签: javascript php jquery ajax ajaxform


【解决方案1】:

Ajax 调用中的类型属性被定义了两次。

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

【讨论】:

  • 我用firebug和console找出问题,现在解决了
【解决方案2】:

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


      });
    }

【讨论】:

  • 不行!
  • 你能分享一下网站文件和文件夹结构吗?然后我会在网址中为您进行更改。
  • 我解决了它只需将我的 jquery 代码放在 jquery 库之后,现在它正在运行......
【解决方案3】:

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

我将我的 javascript 代码文件放在

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

它运行良好。

【讨论】:

    猜你喜欢
    • 2021-10-04
    • 2022-01-22
    • 2020-10-23
    • 2020-09-19
    • 2014-01-17
    • 2015-12-23
    • 2022-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多