react 集成baidu ai 获取token 配置代理

proxy:{
      '/oauth': {
        target: 'https://aip.baidubce.com',//后端接口地址
        changeOrigin: true,
        pathRewrite: {
          '^/oauth': '/oauth'
        }
      },
      '/server_api': {
        target: 'https://vop.baidu.com',//后端接口地址
        changeOrigin: true,
        pathRewrite: {
          '^/server_api': '/server_api'
        }
      },
    },

react 集成baidu ai 获取token 配置代理

getBaiduTokenAjax(postData,fn1,fn2) {
        axios.post( '/oauth/2.0/token',qs.stringify(postData))
            .then(function(data){
                fn1 && fn1(data);
            })
            .catch(function(err){
                fn2 && fn2(err.message);
            });
    }
    getBaiduServer_api_Ajax(postData,fn1,fn2) {
        axios.post('/server_api',postData)
            .then(function(data){
                fn1 && fn1(data);
            })
            .catch(function(err){
                fn2 && fn2(err.message);
            });
    }

react 集成baidu ai 获取token 配置代理

webpackDevServer.js 文件,添加代码段一

业务层,相关为代码段二,也可封装一下

axios.js

const VueAxios = {
  vm: {},
  // eslint-disable-next-line no-unused-vars
  install (Vue, instance) {
    if (this.installed) {
      return
    }
    this.installed = true
    if (!instance) {
      // eslint-disable-next-line no-console
      console.error('You have to install axios')
      return
    }
    Vue.axios = instance
    Object.defineProperties(Vue.prototype, {
      axios: {
        get: function get () {
          return instance
        }
      },
      $http: {
        get: function get () {
          return instance
        }
      }
    })
  }
}
export {
  VueAxios
}

react 集成baidu ai 获取token 配置代理