Ajax+axios案例

Ajax+axios案例

API地址:https://v1.jinrishici.com/

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
    <button id="btn">发送请求</button>
    <h3 id="title"></h3>
    <script>
      var btn = document.getElementById("btn")
      var title = document.getElementById("title")
      // 原生XMLHttpRequest
      btn.onclick = function () {
        var xhr = new XMLHttpRequest()
        xhr.open("get", "https://v1.jinrishici.com/shuqing/aiguo")
        xhr.send()
        xhr.onreadystatechange = function () {
          if (xhr.readyState === 4 && xhr.status === 200) {
            // 获取服务器响应的数据
            console.log(xhr.responseText)
            var rt = JSON.parse(xhr.responseText)
            console.log(rt)
            title.innerText = rt.content
          }
        }
      }
      // axios请求
      //btn.onclick = function () {
      //  const a = axios.get("https://v1.jinrishici.com/shuqing/aiguo").then(
      //          res => {
      //              console.log(res)
      //              // title.innerText = res.data.content
      //          }
      //  ).catch(
      //              err => {
      //                  console.log(err)
      //              }
      //  )
      //}
      // 基于jq的Ajax请求
      //btn.onclick = function () {
      //  $.ajax({
      //    method:'get',
      //    url:"https://v1.jinrishici.com/shuqing/aiguo",
      //    success:function (res) {
      //      console.log(res)
      //
      //      title.innerText = res.content
      //    },
      //    error: function (err) {
      //      console.log(err)
      //    }
      //  })
      //}
    </script>
</body>
</html>
相关推荐
转转技术团队7 小时前
VDOM 编年史
前端·设计模式·前端框架
蓝瑟忧伤7 小时前
前端性能体系的全面升级:现代 Web 如何构建可量化、可治理、可演进的性能架构?
前端·架构
申阳7 小时前
Day 17:03. 基于 Tauri 2.0 开发后台管理系统-登录页面开发
前端·后端·程序员
诸葛亮的芭蕉扇7 小时前
tree组件点击节点间隙的异常问题分析
前端·javascript·vue.js
GinoWi8 小时前
HTML基本格式 - 第一个HTML网页
前端
顶鲜花的牛粪8 小时前
Astro 项目升级全栈:EdgeOne Pages 部署指南
前端
0***R5158 小时前
前端云原生
前端·云原生
月弦笙音8 小时前
【Promise.withResolvers】发现这个api还挺有用
前端·javascript·typescript
疯狂踩坑人8 小时前
MCP理论和实战,然后做个MCP脚手架吧
前端·node.js·mcp
中杯可乐多加冰8 小时前
基于 DeepSeek + MateChat 的证券智能投顾技术实践:打造金融领域的专属大Q模型助手
前端·人工智能