encodeURI 无效 HarmonyOS 鸿蒙Next

encodeURI 无效 HarmonyOS 鸿蒙Next

【设备信息】Mate60

【API版本】Api13

【DevEco Studio版本】5.0.7.200

【问题描述】

HttpRequestOptions -- extraData

发送请求的额外数据,默认无此字段。

当HTTP请求为POST、PUT等方法时,此字段为HTTP请求的content,以UTF-8编码形式作为请求体。当'content-Type'为'application/x-www-form-urlencoded'时,请求提交的信息主体数据必须在key和value进行URL转码后(encodeURIComponent/encodeURI),按照键值对"key1=value1&key2=value2&key3=value3"的方式进行编码,该字段对应的类型通常为String;

encodeURI无效,比如:

GXH8yctxJJJNiWT+sBG3dA==字符串在android平台上进行uri encode后会对字符串中+和=号进行编码,转为%2B和%3D。

但是encodeURI后并不会进行编码,还是保留为+和=。

这样发送http请求form格式时,数据传输到后台,后台收到时候字符串中的+号变成空格了,导致异常。

更多关于encodeURI 无效 HarmonyOS 鸿蒙Next的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

encodeURI()跟安卓转出来的有差别,可以使用encodeURIComponent()

@Entry
@Component
struct EncodeURIDemo {
  @State message: string = 'GXH8yctxJJJNiWT+sBG3dA==';
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(25)
          .fontWeight(FontWeight.Bold)
        Button() {
          Text('encodeURIComponent')
            .fontSize(25)
        }
        .margin({
          bottom: 20
        })
        .type(ButtonType.Capsule)
        .onClick(() => {
          this.message = encodeURIComponent('GXH8yctxJJJNiWT+sBG3dA==');
          console.log('encodeURIComponent======', this.message)
        })
        Button() {
          Text('encodeURI')
            .fontSize(25)
        }
        .margin({
          bottom: 20
        })
        .type(ButtonType.Capsule)
        .onClick(() => {
          this.message = encodeURI('GXH8yctxJJJNiWT+sBG3dA==');
          console.log('encodeURI=========', this.message)
        })
      }
      .width('100%')
    }.height('100%')
  }
}

更多关于encodeURI 无效 HarmonyOS 鸿蒙Next的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,encodeURI无效可能是由于系统对URI编码的处理方式与其他平台不同。HarmonyOS的JavaScript引擎可能在某些情况下对URI编码的实现有差异,导致encodeURI函数无法按预期工作。建议检查代码中URI的生成和处理逻辑,确保符合HarmonyOS的编码规范。此外,可以尝试使用encodeURIComponent对URI的各个部分进行编码,以确保特殊字符被正确处理。如果问题仍然存在,可能需要查阅HarmonyOS的官方文档或API参考,了解系统的具体实现细节。

回到顶部