美文网首页
taro h5跨域问题

taro h5跨域问题

作者: 蚂蚁都督 | 来源:发表于2020-08-14 17:18 被阅读0次

Taro h5 跨域问题

开发模式

h5 发请求会报跨域问题,需要使用代理转换请求。比如后端Rest服务的地址为http://<backend>:<port>

编辑根目录下的 config/index.js 文件

h5: {
    devServer: {
        host: 'localhost',
        port: 10086,
        proxy: {
            '/api/v1': {  // rest服务的path
                target: 'http://<backend>:<port>',  // 服务端地址
                changeOrigin: true
            }
        }
    },
}
  • 代理前的请求:http://localhost:10086/api/v1/user
  • 代理后的请求:http://<backend>:<port>/api/v1/user

部署模式

  h5: {
    devServer:{
      host: "10.0.0.11",//0.0.0.0
      port: 8080,//10086
      // proxy: {
      //   '/v1': {
      //     target: 'http://<backend>:<port>', //不需要proxy的配置
      //     changeOrigin: true
      //   }
      // }
    },

代码里面访问后端服务的地址则

baseUrl = 'http://<backend>:<port>/<path>'   //

相关文章

网友评论

      本文标题:taro h5跨域问题

      本文链接:https://www.haomeiwen.com/subject/bnfndktx.html