黑马axios案例之地区查询

查询某个省内某个城市的所有地区 接口:http://hmajax.itheima.net/api/area

参数名:
pname:省份名字或直辖市名字,比如北京、福建省、辽宁省...
cname:城市名字,比如北京市、厦门市、大连市...

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

<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>

<body>
    省:<input type="text">
    市:<input type="text">
    <button>查询</button>
    <ul>
    </ul>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/1.5.0/axios.js"></script>
<script>
    // http://hmajax.itheima.net/api/area
    // 给按钮设置点击事件
    document.querySelector("button").onclick = function () {
        // 获取省市的input表单值
        let inp = document.querySelectorAll("input")
        let pname = inp[0].value
        let cname = inp[1].value
        axios({
            url: "http://hmajax.itheima.net/api/area",
            params: {
                // 传入省市
                pname,
                cname
            }
        }).then(result => {
            // console.log(result);
            // 处理一下获取到的数据
            let list = result.data.list.map(item => `<li>${item}</li>`).join("")
            // 获取ul
            document.querySelector("ul").innerHTML = list
        })
    }
</script>

</html>

感谢大家的阅读,如有不对的地方,可以向我提出,感谢大家!

相关推荐
酷爱码几秒前
Python虚拟环境与Conda的使用方式详解
开发语言·python·算法
whoarethenext16 分钟前
使用 C++ 和 OpenCV 构建智能答题卡识别系统
开发语言·c++·opencv
张童瑶22 分钟前
Vue Electron 使用来给若依系统打包成exe程序,出现登录成功但是不跳转页面(已解决)
javascript·vue.js·electron
火柴就是我34 分钟前
每日见闻之<script type="module"> 的含义与作用
javascript
m0_5164846738 分钟前
C#winform多选框代码
开发语言·c#
1024小神1 小时前
在rust中执行命令行输出中文乱码解决办法
前端·javascript
*小雪1 小时前
vue2使用vue-cli脚手架搭建打包加密方法-JavaScript obfuscator
前端·javascript·vue.js
CnLiang1 小时前
fnm无缝切换项目的pnpm和node脚本化实践
前端·javascript
ze_juejin1 小时前
JavaScript类型之Symbol
javascript
q567315232 小时前
Java使用Selenium反爬虫优化方案
java·开发语言·分布式·爬虫·selenium