美文网首页
案例:ajax登陆页

案例:ajax登陆页

作者: kino2046 | 来源:发表于2020-02-15 19:43 被阅读0次

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />

  <meta http-equiv="X-UA-Compatible" content="ie=edge" />

  <link rel="stylesheet" href="css/login.css" />

  <title>Document</title>

</head>

<body>

  <div class="loginContainer">

    <h1>登录</h1>

    <form action="/checkUser" method="post">姓名:

      <input class="inputStyle" type="text" name="username" />

      <div class="exchange">用户名错误</div>

      <br />密码:

      <input class="inputStyle" type="password" name="pwd" /><br />

      <input class="loginStyle" type="submit" value="登录" />

    </form>

  </div>

  <script>

    // ajax技术;

    // XMLHttpRequest

    document.querySelector(".inputStyle").onblur = function () {

      // console.log(this.value);

      let xhr = new XMLHttpRequest();

      xhr.open("get", `/checkUserName?username=${this.value}`, true);

      xhr.onload = function () {

        console.log(xhr.responseText);

        let obj = JSON.parse(xhr.responseText);

        document.querySelector(".exchange").innerHTML = obj.info;

        if (obj.status == 1) {

          document.querySelector(".exchange").style.color = "green";

        } else {

          document.querySelector(".exchange").style.color = "red";

        }

      }

      xhr.send();

    }

  </script>

</body>


const Koa = require("koa");

const static = require("koa-static");

const Router = require("koa-router");

const koaBody = require("koa-body");

const fs = require("fs");

const usersData = require("./data/users.json");

let app = new Koa();

app.use(static(__dirname+"/static"));

app.use(koaBody({

    multipart:true

}));

let router = new Router();

router.get("/",(ctx,next)=>{

    ctx.body = "hello";

})

router.get("/checkUserName",(ctx,next)=>{

    console.log(ctx.query.username);

    let res =  usersData.find(v=>v.username==ctx.query.username);

    if(res){

        ctx.body = {

            status:1,

            info:"用户名正确"

        };

    }else{

        ctx.body = {

            status:2,

            info:"用户名错误"

        };

    }

})

router.get("/get/:id",(ctx,next)=>{

    console.log(ctx.params);

    ctx.body = {

        status:1,

        info:"请求成功"

    }

})

router.post("/post",(ctx,next)=>{

    console.log(ctx.request.body);

    ctx.body = {

        status:1,

        info:"post请求成功"

    }

})

router.get("/xml",(ctx,next)=>{

    // ctx.set("content-type","text/xml");

    ctx.body = `<?xml version='1.0' encoding='utf-8' ?>

                    <books>

                        <nodejs>

                            <name>nodejs实战</name>

                            <price>56元</price>

                        </nodejs>

                        <react>

                            <name>react入门</name>

                            <price>50元</price>

                        </react>

                    </books>

                `

})

router.post("/upload",(ctx,next)=>{

    console.log(ctx.request.body);

    console.log(ctx.request.files.img);

     let fileData =  fs.readFileSync(ctx.request.files.img.path);

    fs.writeFileSync("static/imgs/"+ctx.request.files.img.name,fileData);

    ctx.body = "请求成功";

})

router.post("/fileUpload",(ctx,next)=>{

    console.log(ctx.request.files);

    let fileData =  fs.readFileSync(ctx.request.files.myfile.path);

    fs.writeFileSync("static/imgs/"+ctx.request.files.myfile.name,fileData);

    ctx.body = "请求成功";

})

app.use(router.routes());

app.listen(3000);


.loginContainer{

    margin: 0 auto;

    width: 600px;

    text-align: center;

    padding-top: 20px;

    padding-bottom: 50px;

    border: 1px solid;

}

.loginContainer input{

    margin-bottom: 20px;

}

.loginStyle{

    width: 160px;

    height: 40px;

    background: rgb(50,203,77);

    color: white;

    font-size: 17px;

}

.inputStyle{

    width: 200px;

    height: 30px;

    padding: 5px;

    outline: none;

}

.inputStyle:focus{

    border: 1px solid rgb(50,203,77);

}

form{

    position: relative;

}

.exchange{

    position: absolute;

    top:8px;

    right: 65px;

    color: red;

}


[{

    "id":1,

    "username":"张三",

    "pwd":123

},{

    "id":1,

    "username":"李四",

    "pwd":123

}]


相关文章

  • 案例:ajax登陆页

    Document 登录 姓名: 用户名错误 密码: //ajax技术; /...

  • JavaScript : 浅讲ajax

    1.ajax入门案例 1.1 搭建Web环境 ajax对于各位来说,应该都不陌生,正因为ajax的产生,导致前台页...

  • Ajax&Json

    AJAX: JSON: 案例:

  • 27.Ajax & Json

    AJAX: JSON: 案例:

  • 前端登陆调用后端api

    前言 在Django服务器端写了一个API,返回JSON格式数据。前端登陆页面通过Ajax调用该API。 实例 l...

  • ajax

    ajax() ajax的实现 $.ajax({ //发送请求地址(默认当前页地址) url:String, //请...

  • django项目--登陆登出功能

    一、功能需求分析 1、登陆功能分析 1.1、流程 1.2、功能 登陆页面 登陆功能 登出功能 二、登陆页面 1、接...

  • 1.3 ajax流程对象的创建和兼容处理

    3-ajax流程对象的创建和兼容处理 15:31 类似网站上浏览网页的行为,通过案例模拟该行为: 案例3.ajax...

  • 登陆页

  • requests + Beautiful爬取拉勾网职位

    这次的案例中我们会接触到Ajax动态加载的网页,可以看到,包括下一页等很多信息块都没有链接出线,所以就页数遍历上面...

网友评论

      本文标题:案例:ajax登陆页

      本文链接:https://www.haomeiwen.com/subject/bmmyfhtx.html