发表于: 2020-06-03 23:27:21

1 2111


今天完成的事情:今天写了任务六的登录页面
明天计划的事情:继续后续的任务
遇到的问题:理解要加强更加熟练才行
收获:写了登录页面

页面html代码很简单

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./js5-1.css">
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
    </script>
</head>
<body>
    <div class="main">
        <div class="top">后台登录</div>
        <input type="text" id="name" placeholder="用户名">
        <input type="password" id="pwd" placeholder="密码">
        <div id="addText"></div>
        <button id="footer">登录</button>
    </div>
    <script src="./js5-1.js"></script>
</body>
</html>

css代码

html {
    backgroundurl(./3.pngno-repeat;
    background-size100% 100%;
    height100%;
    background-attachment:fixed;
  }
  
  .main {
    displayflex;
    flex-directioncolumn;
    align-contentcenter;
    width400px;
    height320px;
    margin180px auto;
    padding30px 50px;
    border-radius10px;
    background-colorrgba(2502502500.6);
  }
  
  .main .top {
    text-aligncenter;
    font-weight700;
    font-size24px;
    margin-bottom20px;
    /* color: #59b1ec; */
  }
  
  .main input {
    height35px;
    border-radius10px;
    margin-bottom22px;
    padding-left40px;
    background-color#fff !important;
    outline:none;
  }
  #name{
      backgroundurl(./user.pngno-repeat 5px
  }
  
  #pwd{
      backgroundurl(./password.pngno-repeat 5px
  }
  .main #addText {
    height20px;
    colorred;
  }
  
  .main #footer {
    width100%;
    height35px;
    margin-top20px;
    border-radius10px;
    font-size18px;
    text-aligncenter;
    line-height35px;
    outline:none;
  }
  
  

js代码

$("#footer").click(function () {
    $("#addText").text("")
    var name = document.getElementById("name").value//获取值
    var pwd = document.getElementById("pwd").value
    var xhr = new XMLHttpRequest();//异步请求
    xhr.open("post""http://localhost/carrots-admin-ajax/a/login"true)//请求方式
    xhr.setRequestHeader("Content-type""application/x-www-form-urlencoded");//固定写法
    xhr.send("name=" + name + "&pwd=" + pwd);
    xhr.onload = function () {
        let date = JSON.parse(xhr.responseText)
        if (date.code == 0) {
            alert("登录成功")
        } else {
            $("#addText").text(date.message)
        }
    }
    
});

重点是js的部分

var name = document.getElementById("name").value//获取值
    var pwd = document.getElementById("pwd").value

获取输入的值

var xhr = new XMLHttpRequest();//异步请求
    xhr.open("post""http://localhost/carrots-admin-ajax/a/login"true)//请求方式
    xhr.setRequestHeader("Content-type""application/x-www-form-urlencoded");//固定写法

这一部分是ajax的内容,发起请求以及请求方式以及类型的固定写法

xhr.send("name=" + name + "&pwd=" + pwd);

拼接执行的内容

xhr.onload = function () {
        let date = JSON.parse(xhr.responseText)
        if (date.code == 0) {
            alert("登录成功")
        } else {
            $("#addText").text(date.message)
        }
    }

收到服务器响应之后的使用的函数

解析返回内容

账号密码正确就显示登陆成功,否则显示返回内容中的msg

一般是密码错误或者账号不存在

添加到输入框的下面的部分

剩下的明天再继续


返回列表 返回列表
评论

    分享到