首页 > Web开发 > 详细

原生js实现from功能

时间:2020-07-15 00:43:32      阅读:43      评论:0      收藏:0      [点我收藏+]

代码

	<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>XMLHttpReques</title>
    <script>
        var xhr = ‘‘;
        if (window.XMLHttpRequest) {
            xhr = new window.XMLHttpRequest();
        } else {
            xhr = new ActiveXObject(‘Microsoft.XMLHttp‘);
        }
        window.onload = function () {
            document.getElementById("bnt").onclick = function () {
                xhr.open(‘post‘,‘后台端口路径‘,true);

                xhr.setRequestHeader(‘Content-Type‘,‘application/x-www-form-urlencoded‘);
                var username = document.getElementById(‘username‘).value;
                var password = document.getElementById(‘password‘).value;
                xhr.send( ‘memberId=‘+ username + ‘&&password=‘ + password );
                xhr.onreadystatechange = function () {
                    // readyState: 
                    //     0: 请求未初始化
        //                 1: 服务器连接已建立
        //                 2: 请求已接收
        //                 3: 请求处理中
        //                 4: 请求已完成,且响应已就绪
        //             status:
        //                 200: ‘ok‘
        //                 404: 未找到页面或接口
        //                 xhr.responseText: 后端返回的数据
                        if(xhr.readyState == 4 && xhr.status == 200 ) {
                            document.getElementById(‘box‘).innerHTML = xhr.responseText;
                            var json = JSON.parse(xhr.responseText);
                            console.log(json);
                        }

                }
            }
        }
    </script>
</head>
<body>
    <p>账号:<input type="text" id="username"></p>
    <p>密码:<input type="password" name="" id="password"></p>
    <p>&nbsp;&nbsp;&nbsp;<input type="button" value="登录" id="bnt"></p>
    <div id="box"></div>
</body>
</html>

原生js实现from功能

原文:https://www.cnblogs.com/effortfordream/p/13301782.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!