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);
})

.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM