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>
相关推荐
奔跑的呱呱牛1 小时前
xlsx 已停止维护且存在漏洞!推荐一个可直接替代的 npm 库
前端·npm·node.js·xlsx·sheetjs
珑墨1 小时前
pnpm 与 node_modules:硬链接、软连接(符号链接)、Junction 速记
前端
freewlt1 小时前
Monorepo 架构下的前端工程化实践:pnpm + Turborepo 从入门到落地
前端·arcgis·架构
徐小夕9 小时前
我用 AI 撸了个开源"万能预览器":浏览器直接打开 Office、CAD 和 3D 模型
前端·vue.js·github
小码哥_常9 小时前
Flutter Android 延迟加载代码指南:提升应用性能的关键
前端
这是个栗子9 小时前
TypeScript(三)
前端·javascript·typescript·react
kvo7f2JTy9 小时前
基于机器学习算法的web入侵检测系统设计与实现
前端·算法·机器学习
北风toto9 小时前
前端CSS样式详细笔记
前端·css·笔记
nanfeiyan9 小时前
git commit
前端
前端精髓12 小时前
移除 Effect 依赖
前端·javascript·react.js