ArkTS与后端数据库交互过程

安之__
发布于 2023-8-2 10:26
浏览
3收藏

1. 加入网络权限

在module.json5文件中加入网络权限:
“requestPermissions”:[
{
“name”: “ohos.permission.INTERNET”
}
] ,
如图ArkTS与后端数据库交互过程-鸿蒙开发者社区
文件位置:
ArkTS与后端数据库交互过程-鸿蒙开发者社区

2. 导入http

import http from ‘@ohos.net.http’;

3. 书写(以POST方式为例)

let httpRequest = http.createHttp();         //获取HTTP对象
          let url = "http://somewords.xyz:80/store/login"  //填写路径
          let promise = httpRequest.request(
            // 请求url地址
            url,
            {
              // 请求方式
              method: http.RequestMethod.POST,
              // 请求的额外数据。
              extraData: {
                "storeId": this.storeId,           //要携带的参数
                "password": this.storePassword,
              },
              // 可选,默认为60s
              connectTimeout: 60000,
              // 可选,默认为60s
              readTimeout: 60000,
              // 开发者根据自身业务需要添加header字段
              header: {
                'Content-Type': 'application/json'
              }
            }).then((data) => {
                      //回调函数
                  }).catch((err) => {
            console.info('error:' + JSON.stringify(err));
          })
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

已于2023-8-4 10:52:52修改
3
收藏 3
回复
举报
3
2
3
2条回复
按时间正序
/
按时间倒序
安苒anran0
安苒anran0

短小精炼,我喜欢

2
回复
2023-8-2 18:32:57
Crips
Crips

很实用,写的很好


1
回复
2023-8-5 11:11:25


回复
    相关推荐