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>
相关推荐
charlie11451419130 分钟前
从零开始理解 CSS:让网页“活”起来的语言2
前端·css·笔记·学习·选择器·样式表·原生
浪裡遊40 分钟前
Next.js路由系统
开发语言·前端·javascript·react.js·node.js·js
mapbar_front44 分钟前
职场中的顶级能力—服务意识
前端
尽兴-1 小时前
[特殊字符] 微前端部署实战:Nginx 配置 HTTPS 与 CORS 跨域解决方案(示例版)
前端·nginx·https·跨域·cors·chrom
JIngJaneIL2 小时前
助农惠农服务平台|助农服务系统|基于SprinBoot+vue的助农服务系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·助农惠农服务平台
云外天ノ☼2 小时前
待办事项全栈实现:Vue3 + Node.js (Koa) + MySQL深度整合,构建生产级任务管理系统的技术实践
前端·数据库·vue.js·mysql·vue3·koa·jwt认证
gihigo19982 小时前
使用JavaScript和Node.js构建简单的RESTful API
javascript·node.js·restful
一位搞嵌入式的 genius2 小时前
前端实战开发(三):Vue+Pinia中三大核心问题解决方案!!!
前端·javascript·vue.js·前端实战
塞纳河畔的歌2 小时前
保姆级教程 | 麒麟系统安装Edge浏览器
前端·edge
前端.火鸡2 小时前
Vue 3.5 新API解析:响应式革命、SSR黑科技与开发体验飞跃
javascript·vue.js·科技