JSON 教程

JSON 教程

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <a href="https://api.seniverse.com/v3/weather/now.json?key=SRgM4ZKVU_nhcphg3&location=beijing&language=zh-Hans&unit=c">天气</a>
    <form action="114.115.154.154:5111/admin/adminLogin" method="post">
        账号:<input type="text" name="account"><br>
        密码:<input type="password" name="pwd"><br>
        <button type="submit">登录</button>
    </form>
</body>
</html>

json对象

object

复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>json对象</title>
</head>

<body>
    <script>
        let json = {
            //键值对  key:value
            'id': '001',
            'name': '张三的',
            'age': 18
        }
        //数组对象
        let stus =
            [
                { 'id': '001', 'name': '张三', 'age': '18' },
                { 'id': '002', 'name': '张2三', 'age': '19' },
                { 'id': '003', 'name': '张3三', 'age': '16' },
            ]
        //访问
        document.write('<h2>' + stus[0].name + '</h2>')
    </script>
</body>

</html>

实例

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>验证码</title>
    <style>
        img{
            height: 80px;
        }
    </style>
</head>
<body>
    <form action="#">
        <input type="text" name="yzm" placeholder="验证码">
        <img src="img/image.png" alt="">
        <input type="submit" value="提交" class="sb">
    </form>
    <script>
        let yan=''//验证码
        document.querySelector('img').onclick=function(){
            //创建对象
            let xmlReq;
            //检查浏览器是否支持XMLHttpRequest
            if(window.XMLHttpRequest){
                xmlReq=new XMLHttpRequest()
            }else{
                xmlReq=new ActiveXObject()//老版本IE使用
            }
            //发送请求
            xmlReq.open('GET',//提交方式
            //url
            'https://route.showapi.com/26-4?appKey=B62A6E7D02b4494E98De7eD05F23602e',true)
            xmlReq.send()
            //获取响应
            xmlReq.onreadystatechange=function(){
                //响应成功
                if (xmlReq.readyState==4&&xmlReq.status==200) {
                    //xmlhttp.responseText响应的数据  字符串格式
                    // alert(xmlReq.responseText)
                    let str=xmlReq.responseText
                    //把字符串转换成json对象
                    let json=JSON.parse(str)
                    // alert(json)
                    // alert(json.showapi_res_body.img_path_https)
                    document.querySelector('img').setAttribute('src',
                    json.showapi_res_body.img_path_https)
                    yan=json.showapi_res_body.text
                }else{
                    console.log('xmlReq.status'+xmlReq.status);
                    console.log('xmlReq.readyState'+xmlReq.readyState);
                }
            }
        }
        //验证
        document.querySelector('.sb').onclick=function(){
            let val=document.getElementsByName('yzm')[0].value
            if(val===yan){
                alert('提交')
            }else{
                alert('验证失败')
            }
        }
    </script>
</body>
</html>
相关推荐
灵感__idea14 分钟前
Hello 算法:让前端人真正理解算法
前端·javascript·算法
向葭奔赴♡1 小时前
CSS是什么?—— 网页的“化妆师”
前端·css
黑犬mo1 小时前
在Edge、Chrome浏览器上安装uBlock Origin插件
前端·edge
excel1 小时前
🧩 Vue 3 watch 源码详解(含完整注释)
前端·javascript·vue.js
大前端helloworld1 小时前
前端梳理体系从常问问题去完善-网络篇
前端·面试
excel1 小时前
🌿 一文看懂 Vue 3 的 watch 源码:从原理到流程
前端
繁依Fanyi2 小时前
让工具说话:我在 Inspira Board 里用 AI 把“能用、好用、可复用”落成了日常
前端
weixin_456904273 小时前
C# 中的回调函数
java·前端·c#
kura_tsuki3 小时前
[Web网页] LAMP 架构与环境搭建
前端·架构
yinuo3 小时前
UniApp+Vue3多分包引入同一 npm 库被重复打包至 vendor 的问题分析与解决
前端