• 02 【axios fetch 跨域】


    02 【axios fetch 跨域】

    1.axios

    1.1 axios 是什么?

    1. 前端最流行的 ajax请求库
    2. react/vue 官方都推荐使用 axios 发ajax 请求
    3. 文档: https://github.com/axios/axios

    1.2 axios 特点

    1. 基于 xhr + promise 的异步 ajax请求库
    2. 浏览器端/node 端都可以使用
    3. 支持请求/响应拦截器
    4. 支持请求取消
    5. 请求/响应数据转换
    6. 批量发送多个请求

    1.3 axios 常用语法

    axios(config): 通用/最本质的发任意类型请求的方式
    axios(url[, config]): 可以只指定url 发get 请求
    axios.request(config): 等同于axios(config)
    axios.get(url[, config]): 发get 请求
    axios.delete(url[, config]): 发delete 请求
    axios.post(url[, data, config]): 发post 请求
    axios.put(url[, data, config]): 发put 请求

    axios.defaults.xxx: 请求的默认全局配置(method\baseURL\params\timeout…)
    axios.interceptors.request.use(): 添加请求拦截器
    axios.interceptors.response.use(): 添加响应拦截器

    axios.create([config]): 创建一个新的axios(它没有下面的功能)

    axios.Cancel(): 用于创建取消请求的错误对象
    axios.CancelToken(): 用于创建取消请求的 token 对象
    axios.isCancel(): 是否是一个取消请求的错误
    axios.all(promises): 用于批量执行多个异步请求
    axios.spread(): 用来指定接收所有成功数据的回调函数的方法

    image20220625194840070

    1.4 使用

    配置对象常用的配置项
    {
       
      // 路径url
      url: '/user',
    
      // 请求方法,默认get
      method: 'get', 
    
      //基础url,最终请求的url是 baseURL+url拼接,所以再全局设置默认,可以使得发送请求时的url变得简洁
      baseURL: 'https://some-domain.com/api/',
    
      //设置请求头
      headers: {
       'X-Requested-With': 'XMLHttpRequest'},
    
      //设置请求url的query参数,可以使得url简洁。
      //比如url是https://some-domain.com/api/user  然后params如下设置,那么最终的url是:
      //https://some-domain.com/api/user?ID=12345&name=Jack
      params: {
       
        ID: 12345,
        name:"Jack"
      },
    
    
     //设置请求体
      data: {
       
        firstName: 'Fred'
      },
    
      //设置请求的另外一种格式,不过这个是直接设置字符串的
      data: 'Country=Brasil&City=Belo Horizonte',
    
     //请求超时,单位毫秒,默认0,不超时。
      timeout: 1000,
    
      //响应数据类型,默认json
      responseType: 'json', 
    
      //响应数据的编码规则,默认utf-8
      responseEncoding: 'utf8',
    
      //响应体的最大长度 
      maxContentLength: 2000,
    
      // 请求体的最大长度
      maxBodyLength: 2000,
    
      //设置响应状态码为多少时是成功,调用resolve,否则调用reject失败
      //默认是大于等于200,小于300
      validateStatus: function (status) {
       
        return status >= 200 && status < 300; 
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    代码

        <button id="btn1">发送get请求</button> <br><br>
        <button id="btn2">发送post请求</button><br><br>
        <button id="btn3">发送put请求</button><br><br>
        <button id="btn4">发送delete请求</button>
    
        <hr>
    
        <div>其他发送请求的api:</div><br><br>
        <button id="btn5">发送get请求1</button> <br><br>
        <button id="btn6">发送post请求1</button><br><br>
        <button id="btn7">发送put请求1</button><br><br>
        <button id="btn8">发送delete请求1</button>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>
        //发送get
        document.getElementById("btn1").onclick = function(){
       
           axios({
       
            method:"GET",
            url:"http://localhost:3000/posts/1"
           }).then(response=>{
       
               console.log(response);
           })
        };
    
        //发送post
        document.getElementById("btn2").onclick = function(){
       
           axios({
       
            method:"POST",
            url:"http://localhost:3000/posts",
            data:{
       
                title:"axios学习",
                author:"Yehaocong"
            }
           }).then(response=>{
       
               console.log(response);
           })
        };
        //发送put
        document.getElementById("btn3").onclick = function(){
       
           axios({
       
            method:"PUT",
            url:"http://localhost:3000/posts/2",
            data:{
       
                title:"axios学习",
                author:"Liaoxiaoyan"
            }
           }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
  • 相关阅读:
    JavaScript ES6类的定义与继承
    C++10.3 查找第 k 小元素10.4 二叉排序树①
    智慧楼宇联网智能门锁解决方案
    java计算机毕业设计婴幼儿玩具共享租售平台源程序+mysql+系统+lw文档+远程调试
    go的结构体嵌套(组合式继承)
    在Win11上部署ChatGLM3详细步骤
    C++设计模式---原型模式
    go语言学习-异常处理
    Debian 12安装Docker
    Windows11国内首发,宁盾终端准入率先实现兼容
  • 原文地址:https://blog.csdn.net/DSelegent/article/details/127571791