Javascript 如何使用AJAX从数据库检索数据并将结果保存在变量中?

Javascript 如何使用AJAX从数据库检索数据并将结果保存在变量中?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我不熟悉jQuery和AJAX,我正在作为一个项目处理登录页面,我需要使用AJAX从数据库检索数据。我的英语不是100%流利,所以我会在谷歌翻译的帮助下尽力解释这个问题。 以下是我使用的代码: index.html 您需要向document.ready调用和click调用传递函数 <script type="text/javascript"> $(document).ready(function() { Your variables

我不熟悉jQuery和AJAX,我正在作为一个项目处理登录页面,我需要使用AJAX从数据库检索数据。我的英语不是100%流利,所以我会在谷歌翻译的帮助下尽力解释这个问题。 以下是我使用的代码:

index.html
您需要向document.ready调用和click调用传递函数

     <script type="text/javascript">

        $(document).ready(function() {
            Your variables here...

            $('#submit').click(function() {
                ... Ajax call here.
            });
        });
    </script>
您可以使用“提交”,而不是使用“单击事件”

在您的情况下,只需向表单提供id,如-

所以检查你的全部代码-

<!DOCTYPE html>
<html>
  <head>
  <title>Login</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
  </head>
  <body>
    <form validate="" id="submit">
      <input type="text" placeholder="Username" id="username" required/><br />
      <input type="password" placeholder="Password" id="password" required/><br />
      <input type="submit" value="Login" />
    </form>
    <script type="text/javascript">
    // when document is loaded
    $(function() { //shorthand document.ready function
    $('#submit').on('submit', function(e) { 
        e.preventDefault();  //prevent form from submitting
        console.log(data);
        $.ajax({
          type: "POST",
          async: true,
          url: test.php,
          data: $(this).serializeArray(),
          success: function (data) {
            console.log(data);
          }
         });
        });
     });
    </script>
  </body>
</html>

希望这能对你有所帮助。

我认为问题在于你需要再仔细研究一下怎么做。您的单击功能也存在同样的问题。此外,form.submit可能只是在console.log出现之前重新加载页面。非常感谢!这件事我已经琢磨了一个多星期了!你是救命恩人
I've looked at the code and I can't seem to find an error.
Thanks in advance! ;)

>P.S.
>It's probably going to end up being some stupid typo.
>Other than that, have a great day!
     <script type="text/javascript">

        $(document).ready(function() {
            Your variables here...

            $('#submit').click(function() {
                ... Ajax call here.
            });
        });
    </script>
<form validate="" id="submit">
$(function() { //shorthand document.ready function
    $('#submit').on('submit', function(e) { 
        e.preventDefault();  //prevent form from submitting
        console.log(data);
        $.ajax({
          type: "POST",
          async: true,
          url: test.php,
          data: $(this).serializeArray(),
          success: function (data) {
            console.log(data);
          }
        });
    });
});
<!DOCTYPE html>
<html>
  <head>
  <title>Login</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
  </head>
  <body>
    <form validate="" id="submit">
      <input type="text" placeholder="Username" id="username" required/><br />
      <input type="password" placeholder="Password" id="password" required/><br />
      <input type="submit" value="Login" />
    </form>
    <script type="text/javascript">
    // when document is loaded
    $(function() { //shorthand document.ready function
    $('#submit').on('submit', function(e) { 
        e.preventDefault();  //prevent form from submitting
        console.log(data);
        $.ajax({
          type: "POST",
          async: true,
          url: test.php,
          data: $(this).serializeArray(),
          success: function (data) {
            console.log(data);
          }
         });
        });
     });
    </script>
  </body>
</html>