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%')
  }
}
相关推荐
ACP广源盛1392462567321 小时前
IX8024与科学大模型的碰撞@ACP#筑牢科研 AI 算力高速枢纽分享
运维·服务器·网络·数据库·人工智能·嵌入式硬件·电脑
Empty-Filled21 小时前
AI生成测试用例功能怎么测:一个完整实战案例
网络·人工智能·测试用例
码云数智-大飞1 天前
本地部署大模型:隐私安全与多元优势一站式解读
运维·网络·人工智能
jinanwuhuaguo1 天前
(第二十九篇)OpenClaw 实时与具身的跃迁——从异步孤岛到数字世界的“原住民”
前端·网络·人工智能·重构·openclaw
汤愈韬1 天前
三种常用 NAT 的经典案例
网络协议·网络安全·security
等风来不如迎风去1 天前
【win11】最佳性能:fix 没有壁纸,一直黑屏
网络·人工智能
Harvy_没救了1 天前
【网络部署】 Win11 + VMware CentOS8 + Nginx 文件共享服务 Wiki
运维·网络·nginx
汤愈韬1 天前
NAT Server 与目的Nat
网络·网络协议·网络安全·security
2401_873479401 天前
断网时如何实时判断IP归属?嵌入本地离线库,保障风控不中断
运维·服务器·网络
7ACE1 天前
Wireshark TS | TLP 超时时间
网络·网络协议·tcp/ip·wireshark·tcpdump