uni-app 钉钉小程序修复真机使用uni.request发送content-type为application/json的请求时报错
uni-app 钉钉小程序修复真机使用uni.request发送content-type为application/json的请求时报错
开发环境 | 版本号 | 项目创建方式 |
---|---|---|
Windows | win11 | HBuilderX |
产品分类:uniapp/App
PC开发环境操作系统:Windows
HBuilderX类型:正式
HBuilderX版本号:3.96
手机系统:Android
手机系统版本号:Android 13
手机厂商:OPPO
手机机型:oppo find x3 pro
页面类型:vue
vue版本:vue2
打包方式:云端
bug描述:
你们这个bug之前修复过,现在新版本的钉钉又不支持了。
钉钉小程序 修复 真机使用 uni.request 发送 content-type 为 application/json 的请求时报错的Bug
示例代码:
header = {
'Authorization':token,
'content-type': 'application/json;'
};
操作步骤:
header = {
'Authorization':token,
'content-type': 'application/json;'
};
预期结果:
header = {
'Authorization':token,
'content-type': 'application/json;'
};
实际结果:
applicaiton/x-www-form
社区的管理员去哪了?回复下吧
在原生小程序中测试下是否有问题,如有问题请向支付宝社区反馈。
请不要踢皮球,钉钉原生小程序没这问题,现在可以100%确定是uniapp的问题,请麻烦重视下,你们这个bug,在以前的版本中出现过,只是你们说解决了,现在钉钉新版本了,你们又不兼容了
回复 8***@qq.com: 我这里测试是可以的,你是怎么判断实际结果为applicaiton/x-www-form的?
回复 YUANRJ: springBoot后台报的这个错,而且在模拟器没有这问题,真机中才有这问题
回复 8***@qq.com: 回复 DCloud_UNI_YRJ: 通过抓包工具分析,真机高版本不行的原因在于,content-type:application/json; 少了个charset=UTF-8
低版本真机钉钉可以是因为content-type:application/json;charset=UTF-8;
唯一的区别就是 charset=UTF-8
回复 YUANRJ: 加了不管用啊,你们能不能认真点,重视下我反馈的问题
回复 YUANRJ: 你再不认真点,我准备投诉你了
你们当时这个bug是咋解决的
springBoot报这个错
在 uni-app 开发钉钉小程序时,使用 uni.request
发送 Content-Type
为 application/json
的请求时,可能会遇到真机报错的问题。这通常是由于钉钉小程序对请求头的处理方式与微信小程序或其他平台不同导致的。以下是一些可能的解决方案:
1. 检查请求头设置
确保你在 uni.request
中正确设置了 Content-Type
为 application/json
。例如:
uni.request({
url: 'https://example.com/api',
method: 'POST',
header: {
'Content-Type': 'application/json'
},
data: {
key: 'value'
},
success: (res) => {
console.log(res.data);
},
fail: (err) => {
console.error(err);
}
});
2. 使用 dataType
参数
在钉钉小程序中,dataType
参数可能会影响请求的处理方式。尝试将 dataType
设置为 json
:
uni.request({
url: 'https://example.com/api',
method: 'POST',
header: {
'Content-Type': 'application/json'
},
data: {
key: 'value'
},
dataType: 'json',
success: (res) => {
console.log(res.data);
},
fail: (err) => {
console.error(err);
}
});
3. 使用 transformRequest
如果上述方法无效,可以尝试使用 transformRequest
来手动处理请求数据:
uni.request({
url: 'https://example.com/api',
method: 'POST',
header: {
'Content-Type': 'application/json'
},
data: JSON.stringify({
key: 'value'
}),
transformRequest: [function (data, header) {
// 手动处理请求数据
return data;
}],
success: (res) => {
console.log(res.data);
},
fail: (err) => {
console.error(err);
}
});
4. 检查钉钉小程序版本
确保你使用的钉钉小程序 SDK 是最新版本,因为旧版本可能存在一些已知的 bug。你可以通过以下命令更新 SDK:
npm install @dcloudio/uni-mp-dingtalk@latest
5. 使用 dd.httpRequest
如果 uni.request
仍然无法正常工作,可以尝试使用钉钉小程序原生的 dd.httpRequest
方法:
dd.httpRequest({
url: 'https://example.com/api',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
data: {
key: 'value'
},
success: (res) => {
console.log(res.data);
},
fail: (err) => {
console.error(err);
}
});