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>
相关推荐
米丘几秒前
了解 window.history 和 window.location, 更好地掌握 vue-router、react-router单页面路由
前端
swipe4 分钟前
箭头函数与 this 面试题深度解析:从原理到实战
前端·javascript·面试
星_离26 分钟前
《Vue 自定义指令注册技巧:从手动到自动,效率翻倍》
前端·vue.js
狗头大军之江苏分军27 分钟前
消耗 760万 Token 后,一文看懂了“小龙虾” OpenClaw 和 OpenCode 的区别
前端·后端
毛骗导演27 分钟前
万字解析 OpenClaw 源码架构-安全与权限
前端·架构
哇哇哇哇30 分钟前
vue3 ref解析
前端
哇哇哇哇31 分钟前
vue3 reactive解析
前端
光影少年1 小时前
Vue的响应式原理?Vue2和Vue3有什么区别?
前端·vue.js·掘金·金石计划
孟祥_成都1 小时前
Cursor 要被淘汰了?开发者最应该关注的 10 个信号
前端·人工智能
cxxcode1 小时前
Sentry browserTracingIntegration 实现原理深度解析
前端