38js学习第二十二天form请求

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>01form表单请求</title>
</head>
<body>
    <h1>01form表单请求</h1>
    <form action="http://146.56.207.108:3000/Handler/UserHandler?action=login" method="post">
        账号:<input type="text" name="userName"><br>
        密码:<input type="password" name="password"><br>
        <button>提交</button>
    </form>
   
    <form action="https://query.asilu.com/weather/baidu/" method="get">
        输入城市名称:<input type="text" name="city"><br>
        <button>查询天气</button>
    </form>
    <br>
    <form action="http://146.56.207.108:3000/Handler/citylists" method="get">
        <button>获取城市列表</button>
    </form>
    <br>
    <form action="http://146.56.207.108:3000/Handler/citys" method="get">
        请输入要查询的城市:<input type="text" name="name"><br>
        <button>查询</button>
    </form>
    
</body>
<script>
    // 01form表单请求
</script>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>02同步异步</title>
</head>
<body>
    <h1>02同步异步</h1>
</body>
<script>
    // 同步
    // alert(1);
    // console.log(2);

    // 异步
    setTimeout(function(){
        console.log(1)
    },2000)
    console.log(2);
    console.log(3);

</script>
</html>

 <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>03ajax的实例</title>
</head>

<body>
    <h1>03ajax的实例</h1>
    <h2 id="header"></h2>
</body>
<script>
    // 03ajax的实例

    // 01.创建异步请求对象   XMLHttpRequest
    var xhr;
    // 考虑到浏览器的兼容
    if (window.XMLHttpRequest) {
        // 所有现代浏览器(IE7+,FireFox,Chrome,Safari,Opera)
        xhr = new XMLHttpRequest();
    } else {
        // IE5 IE6
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }

    console.log(xhr.readyState);
    // 2.设置请求行   open(method,url,async)
    // method:请求方式  get  post 
    // url:请求地址
    // async:是否异步  true  默认为异步
    // xhr.open("get", "http://146.56.207.108:3000/Handler/citylists", true);

    xhr.open("post", "http://146.56.207.108:3000/Handler/UserHandler?action=login", true);
    // console.log(xhr.readyState);

    // 3.发送请求  send(字符串)
    // get:数据在URL后面用?拼接  不传参数
    // xhr.send();

    // post:会有参数  要发送的数据(以字符串的形式)写在send()中
    // 设置请求头  setRequestHeader()
    xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");

    xhr.send("userName=韩大队&password=123456");
    console.log(xhr.readyState);
    // xhr.readyState  存有XMLHttpRequest的状态   从0到4
    // 0:请求未初始化
    // 1:服务器连接已建立
    // 2:请求已接收
    // 3:请求处理中
    // 4:请求已完成,且响应已就绪
    // xhr.status  状态码

    // onreadystatechange   当状态发生变化时触发
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            console.log(JSON.parse(xhr.response));
        }
        // console.log(xhr.readyState);
        // console.log(xhr.status);
        // console.log(JSON.parse(xhr.response));
        // document.getElementById("header").innerHTML=JSON.parse(xhr.response).data[0].lists
    }

    // JSON.parse()  将字符串类型的对象转换为对象
    // JSON.stringify()   将对象转换为字符串格式

    var json = {
        "name": "李密"
    };
    console.log(JSON.stringify(json))
    var str = '{"name":"李密"}';
    console.log(JSON.parse(str))
</script>

</html>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>04ajax结合form表单进行get请求html</title>
</head>

<body>
    <h1>04ajax结合form表单进行get请求html</h1>
    <button id="btn">获取城市列表</button>
    <ul id="list">


    </ul>
</body>
<script>
    // 04ajax结合form表单进行get请求html

    var oBtn = document.getElementById("btn");
    var oList = document.getElementById("list");
    oBtn.onclick = function () {
        // 01.创建异步请求对象   XMLHttpRequest
        var xhr;
        // 考虑到浏览器的兼容
        if (window.XMLHttpRequest) {
            // 所有现代浏览器(IE7+,FireFox,Chrome,Safari,Opera)
            xhr = new XMLHttpRequest();
        } else {
            // IE5 IE6
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }

        console.log(xhr.readyState);
        // 2.设置请求行   open(method,url,async)
        // method:请求方式  get  post 
        // url:请求地址
        // async:是否异步  true  默认为异步
        xhr.open("get", "http://146.56.207.108:3000/Handler/citylists", true);

        // xhr.open("post", "http://146.56.207.108:3000/Handler/UserHandler?action=login", true);
        // console.log(xhr.readyState);

        // 3.发送请求  send(字符串)
        // get:数据在URL后面用?拼接  不传参数
        xhr.send();

        // post:会有参数  要发送的数据(以字符串的形式)写在send()中
        // 设置请求头  setRequestHeader()
        // xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

        // xhr.send("userName=韩大队&password=123456");
        console.log(xhr.readyState);
        // xhr.readyState  存有XMLHttpRequest的状态   从0到4
        // 0:请求未初始化
        // 1:服务器连接已建立
        // 2:请求已接收
        // 3:请求处理中
        // 4:请求已完成,且响应已就绪
        // xhr.status  状态码

        // onreadystatechange   当状态发生变化时触发
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                var res = JSON.parse(xhr.response)
                // console.log(res);
                if (res.msg == "success") {
                    // console.log(res.data);
                    for (var i = 0; i < res.data.length; i++) {
                        console.log(res.data[i].title)
                        var str = `<li>
                                    ${res.data[i].title}
                                    <ul>
                                        <li></li>
                                        <li></li>
                                        <li></li>
                                    </ul>
                                </li>`
                        oList.innerHTML += str
                    }
                } else {
                    console.log(res.msg)
                }
            }
            // console.log(xhr.readyState);
            // console.log(xhr.status);
            // console.log(JSON.parse(xhr.response));
            // document.getElementById("header").innerHTML=JSON.parse(xhr.response).data[0].lists
        }
    }

    // JSON.parse()  将字符串类型的对象转换为对象
    // JSON.stringify()   将对象转换为字符串格式

</script>

</html>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>05ajax结合form表单进行城市查询</title>
</head>

<body>
    <h1>05ajax结合form表单进行城市查询</h1>
    城市:<input type="text" id="city"><br>
    <button id="btn">查询</button>
    <ul id="list">


    </ul>
</body>
<script>
    // 05ajax结合form表单进行城市查询

    var oBtn = document.getElementById("btn");
    var oCity = document.getElementById("city");
    var oList = document.getElementById("list");
    oBtn.onclick = function () {
        // 01.创建异步请求对象   XMLHttpRequest
        var xhr;
        // 考虑到浏览器的兼容
        if (window.XMLHttpRequest) {
            // 所有现代浏览器(IE7+,FireFox,Chrome,Safari,Opera)
            xhr = new XMLHttpRequest();
        } else {
            // IE5 IE6
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }

        console.log(xhr.readyState);
        // 2.设置请求行   open(method,url,async)
        // method:请求方式  get  post 
        // url:请求地址
        // async:是否异步  true  默认为异步
        xhr.open("get", "http://146.56.207.108:3000/Handler/citys?name="+oCity.value, true);

        // 3.发送请求  send(字符串)
        // get:数据在URL后面用?拼接  不传参数

        xhr.send();

        // xhr.readyState  存有XMLHttpRequest的状态   从0到4
        // 0:请求未初始化
        // 1:服务器连接已建立
        // 2:请求已接收
        // 3:请求处理中
        // 4:请求已完成,且响应已就绪
        // xhr.status  状态码

        // onreadystatechange   当状态发生变化时触发
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                var res = JSON.parse(xhr.response)
                // console.log(res);
                if (res.msg == "success") {
                    // console.log(res.data);
                    for (var i = 0; i < res.data.length; i++) {
                        console.log(res.data[i])
                        var str = `<li>
                                    ${res.data[i].name}
                                </li>`
                        oList.innerHTML += str
                    }
                } else {
                    console.log(res.msg)
                }
            }
            // console.log(xhr.readyState);
            // console.log(xhr.status);
            // console.log(JSON.parse(xhr.response));
            // document.getElementById("header").innerHTML=JSON.parse(xhr.response).data[0].lists
        }
    }

    // JSON.parse()  将字符串类型的对象转换为对象
    // JSON.stringify()   将对象转换为字符串格式

</script>

</html>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>06ajax结合form表单进行post请求</title>
</head>

<body>
    <h1>06ajax结合form表单进行post请求</h1>

    账号:<input type="text" id="user"><br>
    密码:<input type="password" id="psw"><br>
    <button id="login">登录</button>
</body>
<script>
    // 06ajax结合form表单进行post请求


    // 获取元素
    var oUser = document.getElementById("user");
    var oPsw = document.getElementById("psw");
    var oLogin = document.getElementById("login");

    // 点击登录按钮
    oLogin.onclick = function () {
        // 01.创建异步请求对象   XMLHttpRequest
        var xhr;
        // 考虑到浏览器的兼容
        if (window.XMLHttpRequest) {
            // 所有现代浏览器(IE7+,FireFox,Chrome,Safari,Opera)
            xhr = new XMLHttpRequest();
        } else {
            // IE5 IE6
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }

        console.log(xhr.readyState);
        // 2.设置请求行   open(method,url,async)
        // method:请求方式  get  post 
        // url:请求地址
        // async:是否异步  true  默认为异步

        xhr.open("post", "http://146.56.207.108:3000/Handler/UserHandler?action=login", true);
        // console.log(xhr.readyState);

        // post:会有参数  要发送的数据(以字符串的形式)写在send()中
        // 设置请求头  setRequestHeader()

        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

        xhr.send("userName=" + oUser.value + "&password=" + oPsw.value);
        console.log(xhr.readyState);
        // xhr.readyState  存有XMLHttpRequest的状态   从0到4
        // 0:请求未初始化
        // 1:服务器连接已建立
        // 2:请求已接收
        // 3:请求处理中
        // 4:请求已完成,且响应已就绪
        // xhr.status  状态码

        // onreadystatechange   当状态发生变化时触发
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                console.log(JSON.parse(xhr.response));
            }
            // console.log(xhr.readyState);
            // console.log(xhr.status);
            // console.log(JSON.parse(xhr.response));
            // document.getElementById("header").innerHTML=JSON.parse(xhr.response).data[0].lists
        }
    }
</script>

</html>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>07ajax的封装</title>
</head>

<body>
    <h1>07ajax的封装</h1>
</body>
<script>
    // 07ajax的封装

    // 封装:
    // 1.相同部分  不做处理  
    // 2.不同的部分  以参数的形式表示


    // 不同的部分
    // 1.请求地址  url
    // 2.请求方式  method
    // 3.是否异步  async
    // 4.发送的数据  参数  data
    // 5.成功后执行的函数  success
    // 6.失败后执行的函数  fail


    function ajax(url, method, async, data, success, fail) {
        // 01.创建异步请求对象   XMLHttpRequest
        var xhr;
        // 考虑到浏览器的兼容
        if (window.XMLHttpRequest) {
            // 所有现代浏览器(IE7+,FireFox,Chrome,Safari,Opera)
            xhr = new XMLHttpRequest();
        } else {
            // IE5 IE6
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }

        // 2.设置请求行   open(method,url,async)
        // method:请求方式  get  post 
        // url:请求地址
        // async:是否异步  true  默认为异步

        if (method.toLowerCase() === "get") {
            xhr.open("get", url + "?" + data, async);
            // get:数据在URL后面用?拼接  不传参数
            xhr.send();
        } else if (method.toLowerCase() === "post") {
            xhr.open("post", url, async);
            // post:会有参数  要发送的数据(以字符串的形式)写在send()中
            // 设置请求头  setRequestHeader()
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhr.send(data);
        } else {
            console.error("错误", "请求方式错误")
        }
        // xhr.readyState  存有XMLHttpRequest的状态   从0到4
        // 0:请求未初始化
        // 1:服务器连接已建立
        // 2:请求已接收
        // 3:请求处理中
        // 4:请求已完成,且响应已就绪
        // xhr.status  状态码

        // onreadystatechange   当状态发生变化时触发
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    var res = JSON.parse(xhr.response); //将获取到的数据转换为对象
                    success(res)
                } else {
                    fail(xhr.status, xhr.statusText)
                }
            }
        }
    }

    ajax("http://146.56.207.108:3000/Handler/citylists", "get", true, "", function (res) {
        console.log(res)
    }, function (status, statusText) {
        console.log(status, statusText);
    })

    ajax("http://146.56.207.108:3000/Handler/UserHandler?action=login", "post", true, "userName=老韩&password=123456",
        function (res) {
            console.log(res)
        },
        function (status, statusText) {
            console.log(status, statusText);
        })
</script>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值