美文网首页
鸿蒙~与服务端网络通信(二) : 第三方网络请求库 Axios

鸿蒙~与服务端网络通信(二) : 第三方网络请求库 Axios

作者: 胡修波 | 来源:发表于2023-12-28 19:16 被阅读0次

  • axios 是一个基于promise的网络请求库
  • 安装 三方工具 axios
 ohpm install @ohos/axios
  • 移动端:demo: 需要自己二次封装
import axios, { AxiosResponse, FormData } from '@ohos/axios'

const TAG = "huxiubo"
@Entry
@Component
struct Index {
  @State user:gData = null
  @State pData: pData = null;

 // get 请求
  getUserinfo() {
    console.info(TAG, `getUerinfo`);
    axios.get("http://192.168.0.113:8080/userinfo")
      .then((res: AxiosResponse<gData>) => {
        console.info(TAG, `Successed  ${JSON.stringify(res.data)}`);
        this.user = res.data
      })
      .catch((err: Error) => {
        console.error(TAG,  `error: ${JSON.stringify(err)}`)
      })
  }

 // post请求
  postData() {
    let form = new FormData()
    form.append('firstName', 'hu')
    form.append('lastName', 'xiubo')

    console.info(TAG, `${JSON.stringify(form)}`);

    axios.post("http://192.168.0.113:8080/addName", form)
      .then((res: AxiosResponse) => {
        console.info(TAG, `Successed  ${JSON.stringify(res.data)}`);
        this.pData = res.data
        console.info(TAG, `Successed  ${this.pData.fName}`);

      })
      .catch((err: Error) => {
        console.error(TAG,  `error: ${JSON.stringify(err)}`)
      })

  }

  build() {
    Column({space:15}) {
      Text(`get请求结果: ${this.user ? this.user.name :""}`)
      Text(`post请求结果 ${this.pData ? this.pData.fName: ""}`)
      Button("get")
        .onClick(()=> {
          this.getUserinfo()
        })

      Button("post")
        .onClick(()=> {
          this.postData()
        })
    }
    .width('100%')
    .height('100%')
  }
}

interface user {
  firstName:string,
  lastName: string
}

class gData {
  name: string;
  data: string;
}

class pData {
  fName: string;
  lName: string;
}
  • server端 ,采用go ,gin
package main

import (
    "fmt"
    "github.com/gin-gonic/gin"
)

func main() {
    server := gin.Default()
    //get 请求
    server.GET("/userinfo", func(ctx *gin.Context) {
        ctx.JSON(200, gin.H{
            "name": "hongmeng",
            "data": "2023-10-11",
        })
    })
       // post
    server.POST("/addName", func(ctx *gin.Context) {
        firstName := ctx.PostForm("firstName")
        lastName := ctx.PostForm("lastName")
        ctx.JSON(200, gin.H{
            "fName": firstName + "  server",
            "lName": lastName + "  server",
        })

        fmt.Printf(" %s ", firstName)

    })

    server.Run()

}

相关文章

  • axios

    axios axios 是一个专注于网络请求的库! axios 的基本使用:繁 发送get请求: 发送post请求...

  • 2018-12-25

    Android网络请求库的对比 首先,Android端开发离不开与服务端的交互,与服务端交互就离不开网络请求;其次...

  • axios

    Vue官方推荐的网络通信库不再是vue-resource了,推荐使用axios。 axios安装 npm: bow...

  • Vue结合网络数据开发应用axios+vue

    (一)axios网络请求库 为了使用这个js库,应该先进行倒包 axios.get(地址?查询字符串).then(...

  • 网络请求库 – axios库

    1.认识Axios库 Axios 是一个基于 Promise 的 HTTP 库,可以用在浏览器和 node.js ...

  • vue -- 网络应用 -- axios

    axios 网络请求库 axios必须先导入后使用 使用get或post方法即可发送对应的请求 then方法中的回...

  • HTTP库Axios推送损坏的更新,导致数千个网站瘫痪

    Axios 是基于 Promise 的 HTTP 网络请求库,用于浏览器和 Node.js。Axios 体积非常小...

  • 请说明Ajax Fetch Axios三者的区别?

    三者都用于网络请求,但是不同纬度 Ajax,是一种技术统称 Fetch,是一个具体的api Axios,第三方库 ...

  • VUE----脚手架3使用axios

    Axios 是一个基于 promise 的 HTTP 库,也是vue推荐使用的网络请求框架 1.下载axios: ...

  • 初识 axios

    axios 是什么? axios 是基于 promise 的网络请求库,作用于 node.js 和浏览器中。它是 ...

网友评论

      本文标题:鸿蒙~与服务端网络通信(二) : 第三方网络请求库 Axios

      本文链接:https://www.haomeiwen.com/subject/ifmdndtx.html