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数据请求
示例代码如下:
import http from '@ohos.net.http';
@Entry
@Component
struct Index {
build() {
Row() {
Button('发送请求')
.onClick(() => {
this.request()
})
}
}
request() {
let httpRequest = http.createHttp();
httpRequest.request(
"http://192.168.2.20:8081/token",
{
method: http.RequestMethod.GET, // 可选,默认为http.RequestMethod.GET
// 开发者根据自身业务需要添加header字段
header: {
'Content-Type': 'application/json'
},
// 当使用POST请求时此字段用于传递内容
extraData: {},
expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型
}, (err, data) => {
if (!err) {
// 请求成功的处理逻辑
} else {
// 请求失败的处理逻辑
}
}
);
}
}
复制代码
作者:
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