我的功能出了什么问题?

时间:2021-01-09 02:00:04

can't seem to get the login page to work. I need to check the input email/password against the json data i get from the get request. something isn't working, but I can't figure it out.

似乎无法让登录页面工作。我需要根据我从get请求获得的json数据检查输入电子邮件/密码。有些东西不起作用,但我无法弄清楚。

function isSuccess() {
  var firstAdmin = {
    "name": "Admin",
    "email": "admin@example.com",
    "password": "secret",
    "admin": true
  };
  $.post("main.rb", firstAdmin);

  function isValidUser() {
    var json = $.get("/users", function(data) {
      var obj = JSON.parse(json);
      var x = document.form.usermail.value;
      var y = document.form.password.value;

      if (x === obj.users.user.email && y === obj.users.user.password && users.admin === true) {
        window.location = "userprofile/adminprofile.html";
      }
      if (x === users.email && y === users.password && users.admin === false) {
        window.location = "userprofile/userprofile.html";
      }
      alert("success");
    })
  });
}

3 个解决方案

#1


Problem seems to be in the following lines of code

问题似乎出现在以下代码行中

if (x === obj.users.user.email && y === obj.users.user.password && users.admin === true) {
    window.location = "userprofile/adminprofile.html";
}
if (x === users.email && y === users.password && users.admin === false) {
    window.location = "userprofile/userprofile.html";
}

In both if conditions I think users.admin should be repalced with obj.users.user.admin

在两种情况下,我认为user.admin应该使用obj.users.user.admin进行重新处理

and in 2nd if condition users.email should be replaced with obj.users.user.email and same goes for the users.password

如果条件users.email应该用obj.users.user.email替换,那么在第二个用户。密码

#2


If you call

如果你打电话

window.location

you'll get redirected to a new page and the

你将被重定向到一个新页面和

alert("sucess")

won't get called. Have you tried debugging the javascript step-by-step with firefox or chrome developer options?

不会被打电话。您是否尝试过使用firefox或chrome开发人员选项逐步调试javascript?

#3


There is a syntax error in your code

代码中存在语法错误

  });

should just be

应该只是

  }

I should point out that the function isValidUser() can only be called from within the function isSuccess() because it is a function inside another function.

我应该指出函数isValidUser()只能从函数isSuccess()中调用,因为它是另一个函数内的函数。

Another thing to point out is that this appears to be client side JavaScript. And you are providing an API for passwords to be read on the client side, in what appears to be plain text judging from the context. You may want to consider the password authentication to be done on the server side. So your AJAX sends the password and gets a simple yes/no response. The password should not be available in some JSON by going to a url. Obviously this is early days in your development as you don't even appear to have a way to distinguish the user, but I thought I should let you know anyway.

另一点需要指出的是,这似乎是客户端JavaScript。并且您提供了一个API,用于在客户端读取密码,从上下文看,它看起来是纯文本。您可能需要考虑在服务器端进行密码验证。因此,您的AJAX发送密码并获得简单的是/否响应。通过转到URL,密码不应该在某些JSON中可用。显然这是你开发的早期阶段,因为你似乎没有办法区分用户,但我认为无论如何我都应该让你知道。

#1


Problem seems to be in the following lines of code

问题似乎出现在以下代码行中

if (x === obj.users.user.email && y === obj.users.user.password && users.admin === true) {
    window.location = "userprofile/adminprofile.html";
}
if (x === users.email && y === users.password && users.admin === false) {
    window.location = "userprofile/userprofile.html";
}

In both if conditions I think users.admin should be repalced with obj.users.user.admin

在两种情况下,我认为user.admin应该使用obj.users.user.admin进行重新处理

and in 2nd if condition users.email should be replaced with obj.users.user.email and same goes for the users.password

如果条件users.email应该用obj.users.user.email替换,那么在第二个用户。密码

#2


If you call

如果你打电话

window.location

you'll get redirected to a new page and the

你将被重定向到一个新页面和

alert("sucess")

won't get called. Have you tried debugging the javascript step-by-step with firefox or chrome developer options?

不会被打电话。您是否尝试过使用firefox或chrome开发人员选项逐步调试javascript?

#3


There is a syntax error in your code

代码中存在语法错误

  });

should just be

应该只是

  }

I should point out that the function isValidUser() can only be called from within the function isSuccess() because it is a function inside another function.

我应该指出函数isValidUser()只能从函数isSuccess()中调用,因为它是另一个函数内的函数。

Another thing to point out is that this appears to be client side JavaScript. And you are providing an API for passwords to be read on the client side, in what appears to be plain text judging from the context. You may want to consider the password authentication to be done on the server side. So your AJAX sends the password and gets a simple yes/no response. The password should not be available in some JSON by going to a url. Obviously this is early days in your development as you don't even appear to have a way to distinguish the user, but I thought I should let you know anyway.

另一点需要指出的是,这似乎是客户端JavaScript。并且您提供了一个API,用于在客户端读取密码,从上下文看,它看起来是纯文本。您可能需要考虑在服务器端进行密码验证。因此,您的AJAX发送密码并获得简单的是/否响应。通过转到URL,密码不应该在某些JSON中可用。显然这是你开发的早期阶段,因为你似乎没有办法区分用户,但我认为无论如何我都应该让你知道。