Arkts完成数据请求http以及使用axios第三方库
鸿蒙Arkts数据请求
·
import http from '@ohos.net.http'
@Entry
@Component
struct HttpPage {
@State message: string = 'Hello World'
build() {
Column({space:20}) {
Row(){
Button('发送http请求')
.onClick(()=>{
let httpRequest = http.createHttp();
httpRequest.request(
'https://zzgoodqc.cn/index.php/index/qus/getquestionlist',
{
method:http.RequestMethod.POST,
extraData:{
sn:'1001'
}
}
)
.then(resp=>{
console.log("resp=>",JSON.stringify(resp))
if(resp.responseCode === 200){
console.log(resp.result.toString())
}
}).catch(err=>{
console.log('请求错误err=>',err)
})
})
}
}
.width('100%')
.height('100%')
}
}
以上是方案1:默认数据请求
方案二:使用axios第三方库请求接口
第一步:安装aixos,执行
ohpm install @ohos/axios
第二步:注意配置网络权限,在module.json5文件中
"requestPermissions":[
{
"name": "ohos.permission.INTERNET"
}
],
第三步:类似vue,正常引入使用
import axios from '@ohos/axios'
@Entry
@Component
struct HttpPage {
@State message: string = 'Hello World'
build() {
Column({space:20}) {
Row(){
Button('发送axios请求')
.onClick(()=>{
axios.post(
'https://zzgoodqc.cn/index.php/index/qus/getquestionlist',
{
sn:'1001'
}
).then(response=>{
console.log("response=>",JSON.stringify( response))
}).catch(err=>{
console.log('err=>',err)
})
})
}
}
.width('100%')
.height('100%')
}
}

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)