OpenHarmony开发者论坛

标题: OpenHarmony最新的ArkJS中调用http本地服务需要如何配置,如http://192.168.2.20:8081/token [打印本页]

作者: ningquanshui    时间: 2023-10-26 10:28
标题: OpenHarmony最新的ArkJS中调用http本地服务需要如何配置,如http://192.168.2.20:8081/token
[md]OpenHarmony最新的ArkJS中调用http本地服务需要如何配置,如http://192.168.2.20:8081/token

[/md]
作者: hyacinth养花人    时间: 2023-10-26 11:14
标题: OpenHarmony最新的ArkJS中调用http本地服务需要如何配置,如http://192.168.2.20:8081/token
1. 在模块的module.json5文件中配置网络权限
(, 下载次数: 8)
2. 使用http数据请求能力,参考HTTP数据请求
示例代码如下:
  1. import http from '@ohos.net.http';

  2. @Entry
  3. @Component
  4. struct Index {
  5.   build() {
  6.     Row() {
  7.       Button('发送请求')
  8.         .onClick(() => {
  9.           this.request()
  10.         })
  11.     }
  12.   }

  13.   request() {
  14.     let httpRequest = http.createHttp();
  15.     httpRequest.request(
  16.       "http://192.168.2.20:8081/token",
  17.       {
  18.         method: http.RequestMethod.GET, // 可选,默认为http.RequestMethod.GET
  19.         // 开发者根据自身业务需要添加header字段
  20.         header: {
  21.           'Content-Type': 'application/json'
  22.         },
  23.         // 当使用POST请求时此字段用于传递内容
  24.         extraData: {},
  25.         expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型
  26.       }, (err, data) => {
  27.       if (!err) {
  28.         // 请求成功的处理逻辑

  29.       } else {
  30.         // 请求失败的处理逻辑
  31.       }
  32.     }
  33.     );
  34.   }
  35. }
复制代码

作者: ningquanshui    时间: 2023-10-26 11:17
回复 hyacinth养花人: 不行,https的可以,http请求不行

作者: 马迪    时间: 2023-10-26 11:35
[md]有什么错误信息吗?



[/md]
作者: ningquanshui    时间: 2023-10-26 13:41
回复 马迪: 网络连接模式默认https,以前是需要配置 如下,现在不知道怎么配置了
"deviceConfig": {
"network": {
"cleartextTraffic": "true",
"securityConfig": {
"domainSettings": {
"cleartextPermitted": "true",
"domains": [
{
"subdomains": "true",
"name": "guolin.tech"
}
]
}
}
}
}


作者: 马迪    时间: 2023-11-11 07:02
回复 ningquanshui: 配置方式看文档呀:https://docs.openharmony.cn/page ... is/js-apis-http.md/


有啥报错日志?




欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/) Powered by Discuz! X3.5