VUE 开发——AJAX学习(二)

news/2024/9/30 12:24:19 标签: vue.js, 学习, 前端

一、Bootstrap弹框

功能:不离开当前页面,显示单独内容,供用户操作

步骤:

  1. 引入bootstrap.css和bootstrap.js
  2. 准备弹框标签,确认结构
  3. 通过自定义属性,控制弹框显示和隐藏

<head>部分添加:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

<body>结束前添加:

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> 

bootstrap的modal弹框:添加modal类名(默认隐藏) 

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>title</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
    
    <!-- 官网引入:https://v4.bootcss.com/docs/components/modal/ -->
     <!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
    Launch demo modal
  </button>
  
  <!-- Modal -->
  <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          ...
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Save changes</button>
        </div>
      </div>
    </div>
  </div>
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

</body>
</html>

 二、图片上传

  1. 获取图片文件对象
  2. 使用FormData携带图片文件
  3. 提交表单数据到服务器,使用图片url网址

三、 XMLHttpRequest

定义:XHR对象用于与服务器交互,在AJAX编程中被大量使用

步骤:

  1. 创建XHR对象
  2. 配置请求方法和请求URl地址
  3. 监听loadend事件,接收响应结果
<body>
    <script>
        const xhr = new XMLHttpRequest()
        xhr.open('GET','http://hmajax.itheima.net/api/province')
        xhr.addEventListener('loadend',() => {
            console.log(xhr.response)
            //对响应结果做后续处理
        })
        xhr.send()
    </script>
</body>

四、认识Promise 

 Promise:用于表示一个异步操作的最终完成(或失败)及其结果值

成功和失败状态,可以关联对应处理程序

成功调用:resolve(值)触发then()执行

失败调用:reject(值)触发catch()执行

<script>
        /**
         * 使用Promise管理异步任务
         */ 

         //1.创建Promise对象
         const p = new Promise((resolve,reject) => {
            //2.执行异步代码
            setTimeout(() => {
                resolve('成功')
            },2000)
         })
         //3.获取结果
         p.then(result => {
            console.log(result)
         }).catch(error => {
            console.log(error)
         })
         
    </script>

五、Promise三种状态

一个Promise对象,必然处于以下几种状态之一:

待定(pending):初始状态,既没有被兑现,也没有被拒绝

已兑现(fulfilled):操作成功完成

已拒绝(rejected):操作失败

Promise对象一旦被兑现/拒绝,就是已敲定,状态无法再被改变 

六、封装axios函数

<body>
    <p class="my-p"></p>
    <script>
        //1.定义myaxios函数,接收配置对象,返回Promise对象
        function myAxios(config) {
            return new Promise((resolve,reject) => {
                //2.发起xhr请求,默认请求方法为get
                const xhr = new XMLHttpRequest()
                xhr.open(config.method || 'GET',config.url)
                xhr.addEventListener('loadend',() => {
                    //3.调用成功/失败的处理程序
                    if (xhr.status >= 200 && xhr.status < 300) {
                        resolve(JSON.parse(xhr.response))
                    } else {
                        reject(new Error(xhr.response))
                    }
                })
                xhr.send()
                })
        }

        //4.使用myaxios函数,获取省份列表展示
        myAxios({
            url: 'http://hmajax.itheima.net/api/province'
            //其他不写,对于上面的get
        }).then(result => {
            console.log(result)
            document.querySelector('.my-p').innerHTML = result.list.join('<br>')
        }).catch(error => {
            console.log(error)
            document.querySelector('.my-p').innerHTML = error.message
        })
    </script>
</body>

七、 同步代码和异步代码

同步代码:逐行执行,需原地等待结果后,才继续向下执行

异步代码:调用后耗时,不阻塞代码继续执行(不必原地等待),在将来完成后触发一个回调函数;例如setTimeout就是一个异步代码。

JS中的异步代码:

  • setTimeout/setInterval
  • 事件
  • AJAX

异步代码依靠回调函数接收结果

八、Promise——链式调用

使用then函数返回新Promise 对象特性,一直串联下去


http://www.niftyadmin.cn/n/5685092.html

相关文章

maven给springboot项目打成jar包 maven springboot打包配置

基于maven的spring boot 打包分离依赖及配置文件 使用springCloud或springboot的过程中&#xff0c;发布到生产环境的网速受限&#xff0c;如果每次将60,70M甚至更大的jar包上传&#xff0c;速度太慢了&#xff0c;采取jar包和配置文件分离的方式可以极大的压缩jar包大小&#…

js采用覆盖键、覆盖鼠标滑动事件实现禁止网页通过 ctrl + +/- 和 ctrl + 滚轮 对页面进行缩放

一、兼容电脑端的禁止通过 ctrl /- 和 ctrl 滚轮 对页面进行缩放 const keyCodeMap {// 91: true, // command61: true,107: true, // 数字键盘 109: true, // 数字键盘 -173: true, // 火狐 - 号187: true, // 189: true, // -};二、覆盖ctrl||command ‘’/‘-’ // 覆…

H.264编解码工具 - Intel Quick Sync Video

一、简介 Intel Quick Sync Video是英特尔的一个硬件加速技术,用于提高视频编码和解码的性能。它是英特尔处理器中集成的多媒体引擎的一部分。通过利用硬件加速,Quick Sync Video可以大幅提高视频处理性能,同时减少对CPU的负载。 Quick Sync Video支持多种编解码器,包括H…

计算机毕业设计 C语言学习辅导网站的设计与实现 Java实战项目 附源码+文档+视频讲解

博主介绍&#xff1a;✌从事软件开发10年之余&#xff0c;专注于Java技术领域、Python人工智能及数据挖掘、小程序项目开发和Android项目开发等。CSDN、掘金、华为云、InfoQ、阿里云等平台优质作者✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精…

无人机之集群控制及应用

一、无人机集群控制 无人机集群控制是指通过先进的通信、导航和控制算法&#xff0c;实现多架无人机之间的协同、协调和高效的任务执行。其关键技术包括&#xff1a; 通信技术&#xff1a;实现无人机之间的实时数据传输和共享&#xff0c;确保集群控制的准确性和稳定性。 路径…

【大模型系列篇】动手部署实践国产文生图模型-腾讯混元DiT

首个中英双语DiT架构&#xff0c;混元-DiT&#xff0c;高性能细粒度中文理解-多分辨率扩散Transformer模型。 腾讯提出的混元DiT&#xff0c;是一个基于Diffusion transformer的文本到图像生成模型&#xff0c;此模型具有中英文细粒度理解能力。为了构建混元DiT&#xff0c;精心…

采购订单管理:如何驱动业务效率和增长

采购订单是一份具有法律约束力的文件&#xff0c;明确了买方在未来特定日期从供应商处购买商品或服务的意图。 该文件对于买卖双方均具有重要价值。对于买方而言&#xff0c;采购订单有助于其进行未来数月的财务规划&#xff0c;明确资金的支出时间点。对于供应商而言&#x…

鸿蒙开发(NEXT/API 12)【硬件(取消注册出行业务事件监听)】车载系统

取消注册出行业务事件监听。 接口说明 接口名描述[off] (type: ‘smartMobilityEvent’, smartMobilityTypes: SmartMobilityType[], callback?: Callback): void取消注册出行业务事件监听。 开发步骤 导入Car Kit模块。 import { smartMobilityCommon } from kit.CarKit;获…