taro h5跨域问题

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>'   //
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容