blog icon indicating copy to clipboard operation
blog copied to clipboard

vue-cli proxyTable 跨域

Open yongheng2016 opened this issue 6 years ago • 0 comments

vue-cli配置说明

config/index.js

    proxyTable: {
      '/apis': {
          // 测试环境
          target: 'https://www.zhihu.com/',  // 接口域名
          changeOrigin: true,  //是否跨域
          pathRewrite: {
              '^/apis': 'api'   //访问`/apis`等同于访问 https://www.zhihu.com/api
          }              
      }

requst请求

axios.get('/apis/v4/search_v3?t=general&q=test&correction=1&offset=5&limit=10&search_hash_id=ec719636db7492f7d3bca816403049f7').then((res) => {
    console.log(res)
}).catch( (err) => {
    console.log(err)
})

//等同于访问接口
//https://www.zhihu.com/api/v4/search_v3?t=general&q=test&correction=1&offset=5&limit=10&search_hash_id=ec719636db7492f7d3bca816403049f7

yongheng2016 avatar May 12 '18 11:05 yongheng2016