指定要发布呼叫的表单

时间:2015-05-29 18:41:59

标签: javascript jquery ajax post

我目前使用html表单字段进行一次调用。如何指定要使用后调用的值?例如,我想用用户名/密码和用户名/电子邮件进行2次单独的ajax调用。

$('form').submit(function() {
  $.ajax({
    type: "Post",
    url: $('form').attr('action'),
    data: $(this).serialize(),
    success: function(data, status, xhr) {
      alert("Login successful. Redirecting");
      window.setTimeout(function() {
        window.location.href = "3.3.3.3/login"
      }, 5000);

    },
    error: function(data, status, xhr) {
      $('form').trigger("reset");
      alert("Failed to login. Please try again.");
    }
  });

  return false;
  alert("AJAX request successfully completed");
});
<html>

HTML

<head>
  <title>Log In</title>

  <link rel="stylesheet" href="static">
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
</head>

<body>
  <form action="1.1.1.1/login" method="POST">

    <div class="login">
      <div class="login-screen">
        <div class="app-title">
          <h1> Login</h1>
        </div>
        <input class="form-control" type="text" required name="username" placeholder="Username">
        <input class="form-control" type="password" required name="password" placeholder="Password">
        <input class="form-control" type="hidden" required name="eauth" value="pam">
        <input class="form-control" type="text" required name="email" placeholder="email">

        <p>
          <input type="submit" value="Log In" />
        </p>
      </div>
    </div>
  </form>
</body>

</html>

0 个答案:

没有答案