javascript - Ajax 无法用于登录检查

标签 javascript jquery ajax

您好,我不擅长使用ajax。我想检查我的登录信息并返回“成功”或“失败”。购买我的ajax似乎有错误。

var user = $('.username').value();
var pass = $('.password').value();

$.ajax({
  type : 'POST',
  url  : 'login_check.php',
  data : {
    'username': user,
    'password': pass
  },
  beforeSend: function() {
    $("#Loading").show();
  },
  success :  function(response) {     
    if(response=="success" && response!=="fail") {
      $('.status').html("Success! Now logging in ......");
      setTimeout(' window.location.href = "index.php"; ',4000);
    } else {
      $('#Loading i').hide();
      $('.status').html("Login fail! Please use correct credentials....");
      setTimeout(' window.location.href = "login.php"; ',4000);
    }
  }
});

谁能指点一下我吗?

最佳答案

您收到错误的原因是您的 javascript 在 $('.username').value(); 处发生中断(给出错误),因为没有 value() 函数。如果您打开控制台,您会收到此错误。因此,由于脚本的其余部分不起作用。因此,将 $('.username').value(); 更改为 $('.username').val(); ,对于 var 也是如此pass = $('.password').value(); 更改为 var pass = $('.password').val(); 并且您也不需要 if条件如评论中提到的。你的最终代码将是这样的。

var user = $('.username').val();
var pass = $('.password').val();
$.ajax({
    type: 'POST',
    url: //some url
        data: {
            'username': user,
            'password': pass,
        },
    beforeSend: function() {
        //some code
    },
    success: function(response) {
        // some code which you want to excute on success of api
    },
    error: function(xhr, status, error) {
        // some code which you want to excute on failure of api
    }
});

关于javascript - Ajax 无法用于登录检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49340365/

相关文章:

javascript - 如何激活:focus on the input+button when a user click into the input?

javascript - 在 AngularJs SPA 中,如何从 URL 中删除 ".html"

javascript - 拦截所有鼠标事件

java - 在运行时添加 AjaxBehavior 但未调用 AjaxBehaviorListener

javascript - 在 div 中显示名字和姓氏的第一个字母,在 JavaScript 中只有一个输入

javascript - 使用 React Hooks 设置状态的正确方法是什么?

jQuery 对象 : to cache or not to cache?

jquery - jquery 中 $.each 的问题

javascript - 解析私有(private)文件夹(WEB-INf)中的xml

javascript - Liferay - 如何从 @ResourceMapping 带注释的方法渲染 JSP。通过 AJAX 从 JS 文件调用此方法