ArkTS与后端数据库交互过程
安之__
 发布于 2023-8-2 10:26
 浏览
 3收藏
1. 加入网络权限
在module.json5文件中加入网络权限:
“requestPermissions”:[
{
“name”: “ohos.permission.INTERNET”
}
] ,
如图
文件位置:

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));
          })
已于2023-8-4 10:52:52修改
 
        赞
        3
 
        收藏 3
      
 回复
  相关推荐
 




















短小精炼,我喜欢
很实用,写的很好