#鸿蒙通关秘籍#使用axios进行HTTP GET和POST请求

HarmonyOS
2024-12-02 14:22:03
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
SCM晨光吻

在HarmonyOS中进行HTTP请求,首先需要在module.json5文件中设置网络权限:

"requestPermissions": [
      {"name": "ohos.permission.INTERNET"}
    ]

进行GET请求的示例:

import axios from '@ohos/axios';
import { APIs } from './APIs';

const url = APIs.apiPrefix + APIs.getPage;
const params = { 'page': 0, 'limit': 10 };

axios.get(url, { params })
  .then((response) => {
    console.log("GET response data:", response.data);
  })
  .catch((error) => {
    console.log("GET error:", error.message);
  });

进行POST请求的示例:

const url = APIs.apiPrefix + APIs.getSimpleDictList;
const params = { 'page': 0, 'limit': 10 };

axios.post(url, params)
  .then((response) => {
    console.log("POST response data:", response.data);
  })
  .catch((error) => {
    console.log("POST error:", error.message);
  });
分享
微博
QQ
微信
回复
2024-12-02 15:43:01
相关问题
HarmonyOS http 请求 post 参数问题
3050浏览 • 1回复 待解决
HarmonyOS RCP GET请求POST请求如何传参
1330浏览 • 1回复 待解决
HarmonyOS http get请求不到内容
1465浏览 • 1回复 待解决
HarmonyOS http post请求参数传递
1439浏览 • 1回复 待解决
HTTP GET请求时如何传递参数?
4467浏览 • 1回复 待解决
如何使用arkts进行http请求通信
473浏览 • 0回复 待解决
HarmonyOS HTTP-post请求接收不到参数
1347浏览 • 1回复 待解决