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%')
  }
}
相关推荐
qq_409022951 小时前
计算机网络
网络协议·tcp/ip·信息与通信
tan180°1 小时前
Linux网络HTTP(中)(8)
linux·网络·http
锐策2 小时前
深入 RFC 793:TCP 报文头部、MSS 协商与三次握手 / 四次挥手全解析
网络·网络协议·tcp/ip
AORO20254 小时前
防爆手机是什么?2025年防爆手机哪个牌子好?
网络·5g·智能手机·制造·信息与通信
Strawberry_rabbit4 小时前
程序员工作必需之公网和私网
前端·网络协议
milanyangbo4 小时前
从C10K到Reactor:事件驱动,如何重塑高并发服务器的网络架构
服务器·网络·后端·架构
Code_Geo5 小时前
agent设计模式:第二章节—路由
网络·设计模式·路由
月球挖掘机5 小时前
华为USG防火墙之开局上网配置
服务器·网络
噔噔君5 小时前
嵌入式模组拨号获取IP地址时,设置的ippass是什么原理,起到什么作用?
服务器·网络协议·tcp/ip·ip
无敌最俊朗@5 小时前
VMware Ubuntu 虚拟机网络故障分析报告
服务器·网络·ubuntu