Arkts完成数据请求http以及使用axios第三方库

javascript 复制代码
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,执行

javascript 复制代码
ohpm install @ohos/axios

第二步:注意配置网络权限,在module.json5文件中

javascript 复制代码
    "requestPermissions":[
      {
        "name": "ohos.permission.INTERNET"
      }
    ],

第三步:类似vue,正常引入使用

javascript 复制代码
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%')
  }
}
相关推荐
追逐梦想的小孩2 小时前
从零开始学华为:Console口连接设备
网络·华为·交换机
Boilermaker19922 小时前
【Java】网络编程(5)
网络
lpfasd1234 小时前
SSL证书有效期缩短至200天的影响
网络·网络协议·ssl
艾菜籽4 小时前
网络原理-HTTP
网络·网络协议·http
new_daimond5 小时前
微服务-Nacos 技术详解
网络·微服务·架构
zzz.107 小时前
Calico 网络插件在 K8s 集群的作用
网络·云原生·kubernetes
运维行者_7 小时前
OpManager 与 iOS 26:开启 IT 运维新时代
运维·网络·网络协议·网络安全·ios·iphone·告警
云境天合知识分享8 小时前
能见度监测站的优点是什么
网络
筱砚.8 小时前
【数据结构——十字链表】
网络·数据结构·链表
Yvonne爱编码8 小时前
机器人的“神经网络”:以太网技术如何重塑机器人内部通信?【技术类】
网络·机器人