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%')
  }
}
相关推荐
寰天柚子25 分钟前
裸金属服务器深度解析:适用场景、选型指南与运维实践
服务器·网络·github
GTgiantech1 小时前
精准成本控制与单向通信优化:1X9、SFP单收/单发光模块专业解析
运维·网络
Suchadar1 小时前
ACL访问控制列表协议
网络·智能路由器
广东大榕树信息科技有限公司3 小时前
如何通过国产信创动环监控系统优化工厂环境管理?
运维·网络·物联网·国产动环监控系统·动环监控系统
工控小楠3 小时前
EtherNET IP转Profinet协议网关在智能仓储系统中的应用
服务器·网络·tcp/ip
heartbeat..3 小时前
JUC 在实际业务场景的落地实践
java·开发语言·网络·集合·并发
gugugu.4 小时前
Redis ZSet类型深度解析:有序集合的原理与实战应用
网络·windows·redis
车载测试工程师7 小时前
CAPL学习-AVB交互层-功能函数-通用函数
网络·tcp/ip·以太网·capl·canoe
元气满满-樱8 小时前
DHCP服务部署
网络