使用jQuery AJAX对登录表单进行身份验证

时间:2019-02-21 09:29:55

标签: jquery ajax

alert下面的代码中,我给了undefined。我认为问题是由于URL中的反斜杠或其他引起的。 这是json结构

"{
            \"UserName\":\"abc\",
            \"Password\":\"123456\"
}"

如何验证URL中是否存在用户输入的用户名和密码?

<form action="#" id="loginForm">
  <div class="form-group">
    <label for="userName" class="font-weight-bold" style="width:100%;">Username<a href="#" class="float-right font-weight-light axa-color">Forgot?</a></label>
    <input type="text" class="form-control" id="userName" name="userName" placeholder="Enter Email">
  </div>
  <div class="form-group">
    <label for="password" class="font-weight-bold" style="width:100%;">Password<a href="#" class="float-right font-weight-light axa-color">Forgot?</a></label>
    <input type="password" class="form-control" id="password" name="password" placeholder="Enter Password">
  </div>
  <button type="button" id="login_btn" class="btn btn-lg btn-block">LOGIN</button>
</form>
$("#login_btn").click(function() {
  var userName = $("#userName").val();
  var password = $("#password").val();
  authenticate(userName, password);
});

function authenticate(userName, password1) {
  $.ajax({
    type: "POST"
    url: "http://api/User/login",
    dataType: 'json',
    async: false,
    data: {
      username: userName,
      password: password1
    },
    success: function(data) {
      alert(data);
    }
  })
};

1 个答案:

答案 0 :(得分:0)

将数据更改为:

data: {
    userName: userName,
    password: password
},

(您将其写在错误的位置。)之后,您未定义的错误将消失。

在function()中,您还写了password1而不是password