如何使用javascript登录网站?

时间:2019-03-29 21:40:56

标签: javascript html

我正在尝试通过Java脚本登录网站。我在网站上找到了登录表单,不确定从这里开始该怎么做。

我在检查代码时添加了值=“ myemail”和值=“ mypass”,它已使我登录。对于如何实现将Java脚本函数添加到mu自己的代码中,我感到困惑。

asyncio.run
<input name="email" id="emailForSignIn" class="txt-input email float-label ctHidden" type="email" aria-required="true">

2 个答案:

答案 0 :(得分:1)

为了通过Java语言“登录”网站,您必须对表单进行操作。

应设置表单以将您重定向到下一页。

但是,当您对Java有更多的经验时,应该学习用户身份验证。您可以使用第三方服务,也可以自己在后端工作。从您的问题来看,我怀疑您正在学习JS的基础知识。

答案 1 :(得分:0)

您可以使用ajax来做到这一点: https://api.jquery.com/jquery.ajax/

您不需要jQuery,但是它将使事情变得更加容易。

$.ajax({
    type: "POST",
    url: 'login.php', // script to do the actual authentication, set session etc.
    data: {
        username: 'foo', // get element value of username here
        password: 'bar', // get element value of password here
    },
    success: function(data) {
        // process result
    },
});

从技术上讲,您不是通过JS登录,而是使用JS来不重新加载整个页面。

如果您想使用普通的JS登录,则需要在JS中存储凭据,这是可行的,但不是一个好主意:

示例(并非永久存在,每次重新加载页面时都必须登录):

var loggedIn = false;

function authenticate() {
  var password = document.getElementById('password').value;
  
  loggedIn = login(password);
  status();
}

function login(password) {
    var storedPassword = '123';

    return password == storedPassword;
}

function status() {
  if(loggedIn) {
    console.log('You are in :)');
  } else {
    console.log('You are not in :(');
  }
}
<input type='password' value='' id='password'>
<input type='button' onclick='authenticate()' value='login'><br/>
<small>Try 123 :D</small>