uniappx中在使用uni-app uni.request中的header设置了一个{'Tenant-Id':1}的请求头,在小程序中生效了,但安卓中没传递给后端

uniappx中在使用uni-app uni.request中的header设置了一个{‘Tenant-Id’:1}的请求头,在小程序中生效了,但安卓中没传递给后端

项目信息 详细信息
产品分类 uniapp/App
PC开发环境操作系统 Windows
PC开发环境操作系统版本号 wu
HBuilderX类型 正式
HBuilderX版本号 4.76
手机系统 Android
手机系统版本号 Android 16
手机厂商 华为
手机机型 模拟器
页面类型 vue
vue版本 vue3
打包方式 云端
项目创建方式 HBuilderX

示例代码:

function aaa<T>(data:Login):Promise<LoginInfo|null>{
return new Promise((resolve,reject)=>{
const header:UTSJSONObject = {
'Tenant-Id': '1'
}
const options:RequestOptions<any> = {
url: (config as configItem).baseUrl + '/system/wx/bind',
method: 'POST',
data: data,
header:header,
success(res:RequestSuccess<Response<T>>){
const obj:LoginInfo={}
console.log(2222222222)
console.log(res, 11111111111)
resolve(res.data?.data as LoginInfo)
},
fail(){
console.log(3333333333333)
// console.log(err)
reject(null)
},
}
console.log(options)  
uni.request<Response<T>>(options)  
})
}

操作步骤:

预期结果:

实际结果:

bug描述:

uniappx中在使用uni.request中的header设置了一个{‘Tenant-Id’:1}的请求头,在小程序中生效了,但安卓中没传递给后端


更多关于uniappx中在使用uni-app uni.request中的header设置了一个{'Tenant-Id':1}的请求头,在小程序中生效了,但安卓中没传递给后端的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

data 或者 url 中会有重复的吗?

更多关于uniappx中在使用uni-app uni.request中的header设置了一个{'Tenant-Id':1}的请求头,在小程序中生效了,但安卓中没传递给后端的实战教程也可以访问 https://www.itying.com/category-93-b0.html


没有重复的

回复 2***@qq.com: 加一下其它的请求头也都不会携带吗?

这是一个典型的跨平台兼容性问题。在uni-app x中,header参数的数据类型需要特别注意。

问题分析:

  1. 在示例代码中,header被定义为UTSJSONObject类型,但实际赋值使用了JavaScript对象字面量
  2. 小程序平台对数据类型要求较宽松,而Android平台对类型检查更严格
  3. 当header参数类型不匹配时,Android平台可能无法正确序列化请求头

解决方案:

function aaa<T>(data:Login):Promise<LoginInfo|null>{
return new Promise((resolve,reject)=>{
// 使用UTSJSONObject的正确构造方式
const header = new UTSJSONObject()
header.put('Tenant-Id', '1')

const options:RequestOptions<any> = {
url: (config as configItem).baseUrl + '/system/wx/bind',
method: 'POST',
data: data,
header: header, // 使用UTSJSONObject实例
success(res:RequestSuccess<Response<T>>){
const obj:LoginInfo={}
console.log(2222222222)
console.log(res, 11111111111)
resolve(res.data?.data as LoginInfo)
},
fail(){
console.log(3333333333333)
reject(null)
},
}
console.log(options)  
uni.request<Response<T>>(options)  
})
}
回到顶部