html web前端,登录,post请求提交 json带参

html web前端,登录,post请求提交 json带参

3ca9855b3fd279fa17d46f01dc652030.jpg

复制代码
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
    </head>
    <body>
        <div>

            <div style="display: flex; flex-direction: column; margin: 15px;">
                <input id="phone" type="text" autocomplete="off" placeholder="请输入手机号" />
                <input id="code" type="text" autocomplete="off" placeholder="请输入验证码/密码" />
            </div>

            <div style="margin: 15px;">
                <button onClick="login()">登录</button>
            </div>

        </div>
    </body>

    <script type="text/javascript">
        var phoneDom = document.querySelector('#phone'); // 获取输入框内容,手机号码
        var codeDom = document.querySelector('#code'); // 获取输入框内容,验证码/或密码

        /**
         * 提交信息
         * POST发送请求
         */
        function login() {
            ///
            // // 判断手机号码是否正确
            // var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/; // 手机号正则
            // var phone = (phoneDom.value).trim();
            // if (!phoneReg.test(phone)) {
            // alert(" 请输入有效的手机号码"); // 判断手机号码是否正确
            // return false;
            // }

            ///
            // // 判断验证码/或密码是否6位数
            // var code = codeDom.value;
            // if (!code || code.trim().length != 6) {
            // alert("请输入6位短信验证码"); // 判断验证码/或密码是否6位数
            // return false;
            // }

            // 发送 JSON 数据
            var data = {
                phone: phoneDom.value, // 手机号码
                verificationCode: codeDom.value, // 验证码/或密码
            };

            // 创建一个 XMLHttpRequest 对象
            var xhr = new XMLHttpRequest();

            // 配置请求
            xhr.open('POST', 'https://api.wzyanche.com/cusInfo/login', true);
            xhr.setRequestHeader('Content-Type', 'application/json');

            //发送
            xhr.send(JSON.stringify(data));

            // 监听请求的状态,处理返回值
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    if (xhr.status == '200') {
                        //手动对数据转化
                        console.log('222 000 返回的数据', xhr.response);

                        let data1 = xhr.response;
                        console.log('222 111 返回的数据', data1);

                        // 打印,获取json里的对象
                        var data2 = JSON.parse(xhr.responseText)
                        console.log('222 222 返回的数据', data2.retMsg);

                    }
                }
            }
        }
    </script>

</html>
相关推荐
0思必得011 小时前
[Web自动化] HTML列表标签
运维·python·自动化·html·web自动化
烤麻辣烫16 小时前
黑马大事件学习-15(前端登录页面)
前端·css·vue.js·学习·html
询问QQ:48773927817 小时前
Comsol光子晶体仿真:复现子刊论文的奇妙之旅
html
杨超越luckly17 小时前
HTML应用指南:利用GET请求获取全国新东方门店位置信息
前端·数据库·arcgis·html·门店数据
凌波粒18 小时前
CSS基础详解(2)--Grid网格布局详解
前端·css·css3·html5
微风粼粼18 小时前
html对接阿里云百炼AI大模型
人工智能·阿里云·html
明朝百晓生19 小时前
强化学习[page14]【chapter7】Temporal-Difference Learning (TD learning)
前端·html
亿牛云爬虫专家19 小时前
当数据开始“感知页面”
javascript·html·爬虫代理·代理ip·playwright·页面渲染·dom结构
八目蛛20 小时前
色盲测试小游戏
vue.js·vue3·html5
胡gh1 天前
css的臂膀,前端动效的利器,还是布局的“隐形陷阱”?
前端·css·html