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 小时前
c++ libcurl报错Send failed since rewinding of the data stream failed【已解决】
开发语言·c++·http·curl·rewind
喜欢吃豆2 小时前
[特殊字符] 深入解构 Assistants API:从“黑盒”抽象到“显式”控制的架构演进与终极指南
网络·人工智能·自然语言处理·架构·大模型
好望角雾眠2 小时前
第四阶段C#通讯开发-5:TCP
网络·笔记·网络协议·tcp/ip·c#
深圳南柯电子2 小时前
深圳南柯电子|医疗电子EMC整改:助医疗器械安全稳定的关键环节
网络·人工智能·安全·互联网·实验室·emc
报错小能手3 小时前
计算机网络自顶向下方法41——网络层 自治系统内部的路由选择:开放最短路优先(OSPF)设置OSPF链路权值
网络·计算机网络·智能路由器
国科安芯4 小时前
多输出电压条件下同步整流效率测试与优化
网络·单片机·嵌入式硬件·安全
东方隐侠安全团队-千里5 小时前
第3节 RSA算法开启公钥加密时代
网络·人工智能·算法
njnu@liyong6 小时前
HTTP-http是什么?
网络·网络协议·http
李宥小哥7 小时前
结构型设计模式2
网络·数据库·设计模式
xian_wwq7 小时前
【学习笔记】《孙子兵法》与网络安全
网络·笔记·学习