axios 和 vue-axios

1.main.js 全局引入

/**
 * 引入 axios 数据请求方式
 * axios 和 VueAxios 必须同时注入
 */
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)

2.代理 proxyTable

完整接口

https://h5.manhua.163.com/category/1004.json?page=1

设置代理 config/index.js

// 设置代理,解决跨域问题
proxyTable: {
  '/api': {
    target: 'https://h5.manhua.163.com/', // 接口的域名
    secure: false,  // 如果是https接口,需要配置这个参数
    changeOrigin: true, // 如果接口跨域,需要进行这个参数配置
    pathRewrite: {
      '^/api': ''
    }
  }
},

3.页面调用

get 方式

this.axios.get('category/1004.json',{
  params:{
    page: 1
  }
}).then((response) => {
  console.log(response.data.data.books);
}).catch((err) => {
  //
})

post 方式

this.axios.post('category/1004.json',{
  page: 1
},{
  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then((response) => {
  console.log(response);
}).catch((err) => {
  console.log(err);
})

.

原文地址:https://www.cnblogs.com/crazycode2/p/9607918.html