当前位置:   article > 正文

前端 Axios 使用 全局配置 拦截器配置 --Vue中安装Axios_前端全局请求拦截

前端全局请求拦截

目录

Axios 介绍:

引入:

axios基本用法:

axios全局配置:

axios拦截器:

 Vue 中安装 axios:


Axios 介绍:

  1. 基于promise用于浏览器和node.js的http客户端
  2. 支持浏览器和node.js;
  3. 支持promise;
  4. 能拦截请求和响应请求;
  5. 自动转换JSON数据;
  6. 能转换请求和响应数据;

引入:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

axios基本用法:

  1. get和delete请求传递参数
    1. 1 发送get 请求
    2. axios.get('http://localhost:3000/adata').then(function(ret){
    3. // 拿到 ret 是一个对象 所有的对象都存在 ret 的data 属性里面
    4. // 注意data属性是固定的用法,用于获取后台的实际数据
    5. // console.log(ret.data)
    6. console.log(ret)
    7. })
    8. axios delete 请求传参 传参的形式和 get 请求一样
    9. axios.delete('http://localhost:3000/axios', {
    10. params: {
    11. id: 111
    12. }
    13. }).then(function(ret){
    14. console.log(ret.data)
    15. })
    1. 通过传统的url以?的形式传递参数

      1. 2.1 通过传统的url 以 ? 的形式传递参数
      2. axios.get('http://localhost:3000/axios?id=123').then(function(ret){
      3. console.log(ret.data)
      4. })
    2. restful形式传参

      1. 2.2 restful 形式传递参数
      2. axios.get('http://localhost:3000/axios/123').then(function(ret){
      3. console.log(ret.data)
      4. })
    3. 通过params形式传递参数

      1. 2.3 通过params 形式传递参数
      2. axios.get('http://localhost:3000/axios', {
      3. params: {
      4. id: 789
      5. }
      6. }).then(function(ret){
      7. console.log(ret.data)
      8. })

  2. post和put请求传递参数
    1. 4 axios 的 post 请求
    2. 4.1 通过选项传递参数
    3. axios.post('http://localhost:3000/axios', {
    4. uname: 'lisi',
    5. pwd: 123
    6. }).then(function(ret){
    7. console.log(ret.data)
    8. })
    1. 通过选项传递参数

      1. 4.2 通过 URLSearchParams 传递参数
      2. var params = new URLSearchParams();
      3. params.append('uname', 'zhangsan');
      4. params.append('pwd', '111');
      5. axios.post('http://localhost:3000/axios', params).then(function(ret){
      6. console.log(ret.data)
      7. })

    2. 通过URLSearchParams传递参数

      1. 5 axios put 请求传参 和 post 请求一样
      2. axios.put('http://localhost:3000/axios/123', {
      3. uname: 'lisi',
      4. pwd: 123
      5. }).then(function(ret){
      6. console.log(ret.data)
      7. })

axios全局配置:

  1. //配置公共的请求头
  2. axios.defaults.baseURL = 'https://127.0.0.1:3000/api'
  3. // 配置超时时间
  4. axios.defaults.timeout = 3000;
  5. // 配置公共请求头
  6. axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
  7. //配置公共的post的Content-Type
  8. axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

axios拦截器

  1. 请求拦截器:
    1. 请求拦截器的作用是在请求发送前进行一些操作,
    2. 例如:在每个请求体中加上token,统一做了处理 后要改也很简单
    3. //1.请求拦截器
    4. axios.interceptors.request.use(function(config){
    5. console.log(config.url)
    6. // 任何一部都会经过这一步,在发送请求前做些什么
    7. config.headers.mytoken = 'token字符串'
    8. // 这里一定要return 否则配置不成功
    9. return config
    10. },function(err){
    11. //请求错误的时候
    12. console.log(err)
    13. })

  2. 响应拦截器:
    1. 响应拦截器的作用是在接收到响应后进行的一些操作,
    2. 如:在服务器返回登录状态失效,需要重新 登录的时候,跳转到登录页;
    3. //2.响应拦截器
    4. axios.interceptors.response.use(function(res){
    5. // 接收响应后做些什么
    6. var data = res.data;
    7. return data;
    8. },function(err){
    9. //打印错误
    10. console.log(err)
    11. })

 Vue 中安装 axios:

  1. 安装依赖:(在控制台)
    npm i axios -S
  2. 配置:
    1. 在main.js中导入 axios
    2. import axios from 'axios'
    3. import Vue from 'vue'
    4. 在全局挂载 axios 方便在各个组件使用
    5. Vue.prototype.$axios = axios
    6. new Vue({
    7. render: h => h(App),
    8. }).$mount('#app')
  3. 对axios进行二次封装:

    1. 导入依赖
    2. import axios from 'axios'
    3. 配置属性
    4. const service = axios.create({
    5. baseURL: '......',
    6. timeout: 3000,
    7. })
    8. export default service

    配置拦截器:

    1. // 请求拦截器
    2. // 在发送请求前 操作
    3. service.interceptors.request.use((config) => {
    4. // 获取 并 在请求头设置 token
    5. config.headers['token'] = getToken('token')
    6. return config
    7. }, (error) => {
    8. return Promise.reject(error)
    9. })
    10. // 响应拦截器
    11. // 对响应数据的操作
    12. service.interceptors.response.use((response) => {
    13. // 结构请求的数据
    14. let { status, message } = response.data
    15. if (status !== 200) {
    16. Message({ message: message || 'error', type: 'error' })
    17. }
    18. return response
    19. }, (error) => {
    20. return Promise.reject(error)
    21. })

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号