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%')
  }
}
相关推荐
Multipath71217 分钟前
5G CPE vs 5G聚合路由器 该怎么选
网络·5g·安全·智能路由器·实时音视频·信息与通信
发光小北41 分钟前
Bronze100 系列 自研PLC你用过吗?
网络协议
xiaoxiangsiyan2 小时前
网络智能化转型核心模块全解析
运维·服务器·网络·数据库·学习·架构·php
盟接之桥3 小时前
半导体供应链破局:EDI如何成为中国制造的数字通行证
大数据·运维·服务器·网络·数据库·人工智能·制造
MartinYeung53 小时前
[论文学习]智能合约不变式防御网络犯罪
网络·学习·智能合约
Patrick_Wilson3 小时前
RPC 与 REST 的本质区别看这一篇就够了:别再只看 URL 里有没有动词
http·rpc·restful
学习星球3 小时前
6G核心网架构深度解析——AI Native时代的网络变革
网络·人工智能·5g·架构·go·信息与通信
华允物联-HUAIOT3 小时前
基于网络IO的水泵远程控制系统方案
网络·物联网
我爱cope3 小时前
【计算机网络 | 网络层10:OSPF 与 BGP:自治系统内部和互联网之间如何路由?】
网络·学习·计算机网络·智能路由器