【Ajax】发送get请求获取接口数据

编写html实现通过Ajax发送get请求并获取数据

代码实现
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Index</title>
</head>
<body>
    <br>
    <h2 style="text-align: center;">获取数据</h2>
    <div style="text-align: center;">
        <input type="button" value="获取" onclick="getData()" />
        <div id="textArea">

        </div>
    </div>
</body>
<script>
    function getData(){
        // 创建请求
        var xhttp = new XMLHttpRequest();
        // 定义请求
        xhttp.open("GET", "https://mock.apifox.cn/m1/3038469-0-default/emp/list", true)
        // 回调函数,执行完请求后,会将数据返回来
        xhttp.onreadystatechange = function(){
            if(this.readyState == 4 && this.status == 200){
                // this.responseText发送请求后,响应过来的数据
                document.getElementById("textArea").innerHTML = this.responseText;
            }
        };
        // 发送请求
        xhttp.send();
    }
</script>
</html>
结果
  • 运行编写好的html页面,点击获取数据按钮,即可获取到对应接口返回的数据
相关推荐
剑亦未配妥25 分钟前
移动端触摸事件与鼠标事件的触发机制详解
前端·javascript
人工智能训练师6 小时前
Ubuntu22.04如何安装新版本的Node.js和npm
linux·运维·前端·人工智能·ubuntu·npm·node.js
Seveny076 小时前
pnpm相对于npm,yarn的优势
前端·npm·node.js
yddddddy7 小时前
css的基本知识
前端·css
昔人'7 小时前
css `lh`单位
前端·css
破无差8 小时前
《赛事报名系统小程序》
小程序·html·uniapp
Nan_Shu_6149 小时前
Web前端面试题(2)
前端
知识分享小能手9 小时前
React学习教程,从入门到精通,React 组件核心语法知识点详解(类组件体系)(19)
前端·javascript·vue.js·学习·react.js·react·anti-design-vue
蚂蚁RichLab前端团队10 小时前
🚀🚀🚀 RichLab - 花呗前端团队招贤纳士 - 【转岗/内推/社招】
前端·javascript·人工智能
孩子 你要相信光10 小时前
css之一个元素可以同时应用多个动画效果
前端·css