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>
相关推荐
摇滚侠6 小时前
11 空间转换 前端 Web 开发 HTML5 + CSS3 + 移动 web 视频教程,前端web入门首选黑马程序员
前端·css·html·css3·html5
小李子呢02117 小时前
前端八股CSS---CSS布局
css·html·css3
Beginner x_u10 小时前
前端八股整理总索引|JS/TS、HTML/CSS、Vue、浏览器、工程化与手写题
前端·javascript·html
T畅N1 天前
审批流设计器(前端)
前端·elementui·vue·html·流程图·js
ZC跨境爬虫1 天前
跟着 MDN 学 HTML day_17:媒体与 Web Audio API 自动播放指南——策略、检测与最佳实践
前端·笔记·ui·html·音视频·媒体
水云桐程序员1 天前
前端教程官方文档|HTML、CSS、JavaScript教程官方文档
前端·javascript·css·html·学习方法
JackieDYH1 天前
CSS Flexbox 与 Grid 的默认行为-布局的底层机制
前端·css·html
菜蒙爱学习1 天前
【Markdown】可用的所有 HTML 标准颜色
前端·html
DTcode71 天前
新的改变 # 2026前端避坑指南:CSS继承乱成一锅粥?inherit、initial和unset三招教你原地封神
html·面试宝典·web前端·网页开发
yanchGod1 天前
CSS page-break-before 属性详解:控制打印分页的艺术
前端·javascript·css·html·css3·html5