zoukankan      html  css  js  c++  java
  • axios的拦截请求与响应

    // 请求拦截(配置发送请求的信息)
    axios.interceptors.request.use(function (config) {
      // 处理请求之前的配置
      return config
    }, function (error) {
      // 请求失败的处理
      return Promise.reject(error)
    })
    
    // 响应拦截(配置请求回来的信息)
    axios.interceptors.response.use(function (response) {
      // 处理响应数据
      return response
    }, function (error) {
      // 处理响应失败
      return Promise.reject(error)
    })
    
    // axios转发 //其他页面在使用axios的时候直接  this.$axios就可以了
    Vue.prototype.$axios = axios

    调用

      methods: {
        getInfo () {
          this.$axios.get('/fcg_myqq_toplist.fcg', {
            headers: {
              'authorization': this.token
            },
            params: {
              g_tk: 5381,
              uin: 0,
              format: 'json',
              inCharset: 'utf-8',
              outCharset: 'utf-8',
              notice: 0,
              platform: 'h5',
              needNewCode: 1
            }
          }).then((res) => {
            this.discList = res.data.data.topList
            console.log(this.discList)
          }).catch((err) => {
            console.log(err.response.status)
          })
        }
      },

    configindex.js配置跨域

        proxyTable: {
          '/api':{
            target: "https://c.y.qq.com/v8/fcg-bin",
            changeOrigin:true,
            pathRewrite:{
              '^/api':''
            }
          }
        },

    main.js

    Vue.prototype.$axios = axios
    axios.defaults.baseURL = '/api'
    axios.defaults.headers.post['Content-Type'] = 'application/json'
  • 相关阅读:
    题解 P5320
    Codeforces 1500F
    三个 AGC D(AGC037D、AGC043D、AGC050D)
    Atcoder Regular Contst 084 D
    DG-基础知识点整理
    MySQL-数据恢复场景实验
    MySQL-查看Galera集群状态
    MySQL-运行日志切割
    MySQL-生产环境删除大表或大量binlog策略
    MySQL-基于(MySQL 5.7)NDB中启用共享权限表
  • 原文地址:https://www.cnblogs.com/ronle/p/10970043.html
Copyright © 2011-2022 走看看