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%')
  }
}
相关推荐
网硕互联的小客服1 小时前
如何安全配置数据库(MySQL/PostgreSQL/MongoDB)
linux·运维·服务器·网络·windows
安全系统学习4 小时前
网络安全之Web渗透加解密
网络·安全·web安全·网络安全·系统安全
枷锁—sha4 小时前
WireShark网络抓包—详细教程
网络·测试工具·web安全·网络安全·wireshark
Ankie Wan4 小时前
tshark的使用技巧(wireshark的命令行,类似tcpdump):转换格式,设置filter
网络·测试工具·wireshark·tcpdump·tshark
有个抽抽4 小时前
本地项目如何设置https(2)——2025-05-19
网络协议·http·https
长流小哥5 小时前
STM32:Modbus通信协议核心解析:关键通信技术
服务器·网络·stm32·单片机·嵌入式硬件·信息与通信·modbus
Shaun_青璇6 小时前
物联网 温湿度上传onenet
网络·物联网
海尔辛7 小时前
知道Metasploit 吗?
网络·学习·测试工具·metasploit
无名之逆7 小时前
WebSocket Broadcasting with hyperlane
网络·websocket·网络协议·http·rust
枷锁—sha7 小时前
【HW系列】—web常规漏洞(CSRF与SSRF)
服务器·前端·网络·笔记·安全·网络安全·csrf