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>
相关推荐
xhxxx1 小时前
别再被 CSS 定位搞晕了!5 种 position 一图打尽 + 实战代码全公开
前端·css·html
武清伯MVP2 小时前
深入了解Canvas:HTML5时代的绘图利器(二)
前端·html5·canvas
询问QQ688238863 小时前
基于偏最小二乘算法(PLS)的多输出数据回归预测
html
挫折常伴左右5 小时前
初见HTML
前端·html
武清伯MVP1 天前
深入了解Canvas:HTML5时代的绘图利器(一)
前端·html5·canvas
一水鉴天1 天前
整体设计 定稿 之24 dashboard.html 增加三层次动态记录体系仪表盘 之2 程序 (Q208 之1)
前端·html
一水鉴天1 天前
整体设计 定稿 之22 dashboard.html 增加三层次动态记录体系仪表盘 之1
前端·html
沟通QQ8762239651 天前
有限元仿真模型仿真模型-基于COMSOL多物理场耦合仿真的变压器流固耦合及振动噪声分析 1、变...
html
江公望1 天前
HTML5 History 模式 5分钟讲清楚
前端·html·html5
A24207349301 天前
使用jQuery动态操作HTML和CSS
css·html·jquery