HarmonyOS鸿蒙Next中穿戴设备试图申请http请求出现 "TypeError: Cannot read property 'createHttp' of undefined"
HarmonyOS鸿蒙Next中穿戴设备试图申请http请求出现 “TypeError: Cannot read property ‘createHttp’ of undefined” 为什么我在 Lite Wearable 项目上 想请求http服务,导入的http命名空间永远都是undefined呢?
提示"TypeError: Cannot read property ‘createHttp’ of undefined"
API使用 4.0.0(10)
JS fasMode



更多关于HarmonyOS鸿蒙Next中穿戴设备试图申请http请求出现 "TypeError: Cannot read property 'createHttp' of undefined"的实战教程也可以访问 https://www.itying.com/category-93-b0.html
函数不支持
我实在是找不到可联网的方法了
我实在是找不到可联网的方法了
该错误表明HTTP请求模块未正确导入。HarmonyOS穿戴设备需使用@ohos.net.http模块。在代码顶部添加import http from '@ohos.net.http';。createHttp()是http对象的方法,若http为undefined,确认已安装对应SDK版本且API等级≥9。同时检查module.json5中是否声明了ohos.permission.INTERNET权限。
HarmonyOS NEXT 中 Lite Wearable(轻量级穿戴设备)项目使用的是 JS FA(Feature Ability) 模型,该模型不直接支持标准 @ohos.net.http 模块。
原因:
- 轻量级穿戴设备的系统能力集(SystemCapability)有限,未裁剪
@ohos.net.http的实现。 - 在 JS FA 模式下,
import http from '@ohos.net.http'会返回undefined,因为该模块的底层原生接口未注册到 JS 运行时。
当前实现方式:
- 穿戴设备必须使用
[@system](/user/system).fetch接口(属于 JS FA 内置的通用能力)发起 HTTP 请求。 - 示例代码:
import fetch from '[@system](/user/system).fetch'; fetch.fetch({ url: 'http://example.com/api', method: 'GET', success: function(data) { console.log('Success' + JSON.stringify(data)); }, fail: function(data, code) { console.log('Fail: ' + code); } });
注意事项:
[@system](/user/system).fetch是 JS FA 专属 API,不支持 Promise 语法,需使用回调。- 确保在
config.json中声明network权限(ohos.permission.INTERNET)。 - 穿戴设备通常只支持 HTTP,不支持 HTTPS(因证书栈资源限制)。
代码中 import http from '@ohos.net.http' 在 Lite Wearable 项目上永远无法工作,需替换为 import fetch from '[@system](/user/system).fetch'。


