uni-app 打包app设置Referer无效

uni-app 打包app设置Referer无效

类别 信息
产品分类 uniapp/App
PC开发环境 Windows
PC版本号 windows11
HBuilderX 正式
HBuilderX版本 4.75
手机系统 Android
手机系统版本 Android 16
手机厂商 华为
手机机型 安卓苹果所有机型
页面类型 vue
vue版本 vue2
打包方式 云端
项目创建方式 HBuilderX

操作步骤:

  • 在请求头里添加Referer来源字段

预期结果:

  • 发送请求时,请求头上携带Referer来源字段

实际结果:

  • 请求头中无Referer来源字段

bug描述:

  • 图片资源开启阿里云防盗链功能,uniapp 打包app设置Referer无效,导致资源无法访问。

更多关于uni-app 打包app设置Referer无效的实战教程也可以访问 https://www.itying.com/category-93-b0.html

9 回复

怎么写的,提供一下代码看看

更多关于uni-app 打包app设置Referer无效的实战教程也可以访问 https://www.itying.com/category-93-b0.html


1.template.h5.html页面,<meta name="referrer" content="no-referrer-when-downgrade"> 2. uni.request请求中添加config.header[‘Referer’] = ‘xxxxxxxxx’

回复 2***@qq.com: 没懂你是如何使用的,提供一下复现项目吧

回复 DCloud_UNI_yuhe: uniapp开发的app项目怎么添加Referer,用来请求oss图片资源

回复 2***@qq.com: 实在 request 中添加吗?

回复 DCloud_UNI_yuhe: 是的,接口请求的时候需要带上

回复 DCloud_UNI_yuhe: 如果不带上,阿里云oss的防盗链会导致,图片无法正常显示

回复 2***@qq.com:header 中不能设置 Referer ,另外阿里云防掉链的文档在哪里?贴出来看一下

在uni-app中设置Referer头字段需要注意几个关键点:

  1. 平台差异:uni-app的App端使用的是原生网络请求,与浏览器环境不同。在App端,Referer默认不会自动携带,需要手动设置。

  2. 正确设置方式

    • 使用uni.request时,在header中明确添加Referer字段:
    uni.request({
      url: 'https://example.com/resource',
      header: {
        'Referer': 'https://your-domain.com'
      },
      success: (res) => {
        console.log(res.data);
      }
    });
回到顶部